local M=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local o="\068\101\102\097\117\108\116\083\111\117\110\100\069\118\101\110\116\115"if(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\085\115\101\083\111\117\110\100\068\105\115\112\097\116\099\104\101\114")then local I={}local l=M:FindFirstChild(o)if not l then l=Instance.new("\070\111\108\100\101\114")l.Name=o l.Archivable=false l.Parent=M end local function U(M,o)local I=l:FindFirstChild(M)if not I then I=Instance.new(o)I.Name=M I.Parent=l end return I end local Y=U("\068\101\102\097\117\108\116\083\101\114\118\101\114\083\111\117\110\100\069\118\101\110\116","\082\101\109\111\116\101\069\118\101\110\116")local D=U("\065\100\100\067\104\097\114\097\099\116\101\114\076\111\097\100\101\100\069\118\101\110\116","\082\101\109\111\116\101\069\118\101\110\116")local t=U("\082\101\109\111\118\101\067\104\097\114\097\099\116\101\114\069\118\101\110\116","\082\101\109\111\116\101\069\118\101\110\116")local function k(M,o,l,U)if I[M]then Y:FireClient(M,o,l,U)end end local function i(M)I[M]=true end local function P(M)I[M]=nil end local H=U("\083\111\117\110\100\068\105\115\112\097\116\099\104\101\114","\066\105\110\100\097\098\108\101\069\118\101\110\116")H.Event:Connect(k)Y.OnServerEvent:Connect(function() end)D.OnServerEvent:Connect(i)t.OnServerEvent:Connect(P)end

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SOUND_EVENT_FOLDER_NAME = "DefaultSoundEvents"

if UserSettings():IsUserFeatureEnabled("UserUseSoundDispatcher") then
	local loadedCharacters = {}

	local EventFolder = ReplicatedStorage:FindFirstChild(SOUND_EVENT_FOLDER_NAME)
	if not EventFolder then
		EventFolder = Instance.new("Folder")
		EventFolder.Name = SOUND_EVENT_FOLDER_NAME
		EventFolder.Archivable = false
		EventFolder.Parent = ReplicatedStorage
	end

	local function createEvent(name, instanceType)
		local newEvent = EventFolder:FindFirstChild(name)
		if not newEvent then
			newEvent = Instance.new(instanceType)
			newEvent.Name = name
			newEvent.Parent = EventFolder
		end

		return newEvent
	end

	local DefaultServerSoundEvent = createEvent("DefaultServerSoundEvent", "RemoteEvent")
	local AddCharacterLoadedEvent = createEvent("AddCharacterLoadedEvent", "RemoteEvent")
	local RemoveCharacterEvent = createEvent("RemoveCharacterEvent", "RemoteEvent")

	-- Fire the sound event to all clients connected
	local function fireDefaultServerSoundEventToClient(player, sound, playing, resetPosition)
		if loadedCharacters[player] then
			DefaultServerSoundEvent:FireClient(player, sound, playing, resetPosition)
		end
	end

	-- Add a character to the list of clients ready to receive sounds
	local function addCharacterLoaded(player)
		loadedCharacters[player] = true
	end

	-- Remove a character from the table
	local function removeCharacter(player)
		loadedCharacters[player] = nil
	end

	local soundDispatcher = createEvent("SoundDispatcher", "BindableEvent")
	soundDispatcher.Event:Connect(fireDefaultServerSoundEventToClient)

	--no op function to prevent rogue client from filling RemoteEvent queue
	DefaultServerSoundEvent.OnServerEvent:Connect(function() end)
	AddCharacterLoadedEvent.OnServerEvent:Connect(addCharacterLoaded)
	RemoveCharacterEvent.OnServerEvent:Connect(removeCharacter)
end
