local z=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local h="\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 U={}local k=z:FindFirstChild(h)if not k then k=Instance.new("\070\111\108\100\101\114")k.Name=h k.Archivable=false k.Parent=z end local function V(z,h)local U=k:FindFirstChild(z)if not U then U=Instance.new(h)U.Name=z U.Parent=k end return U end local b=V("\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 g=V("\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 l=V("\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 e(z,h,k,V)if U[z]then b:FireClient(z,h,k,V)end end local function r(z)U[z]=true end local function Z(z)U[z]=nil end local F=V("\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")F.Event:Connect(e)b.OnServerEvent:Connect(function() end)g.OnServerEvent:Connect(r)l.OnServerEvent:Connect(Z)end
--[[
	The sound dispatcher will fire sound events to properly loaded characters. This script manages a list of
	characters currently loaded in the game. When a character fires a sound event, this dispatcher will
	check to make sure the event only fires on characters who have loaded in.
--]]
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
