local function m(m,C,r,v,Z)return((m-C)*(Z-v))/(r-C)+v end local function C(m)m.TimePosition=0 m.Playing=true end local function r(m)local C={}for m,r in pairs(m)do C[m]=r end return C end local function v(v,Z,G)local z={}for m,C in pairs(SOUND_DATA)do local r=Instance.new("\083\111\117\110\100")r.Name=m r.Archivable=false r.EmitterSize=5 r.MaxDistance=150 r.Volume=.65 for m,C in pairs(C)do r[m]=C end r.Parent=G z[m]=r end local Y={}local function y(m)for C in pairs(r(Y))do if C~=m then C.Playing=false Y[C]=nil end end end local A={[Enum.HumanoidStateType.FallingDown]=function()y()end,[Enum.HumanoidStateType.GettingUp]=function()y()C(z.GettingUp)end,[Enum.HumanoidStateType.Jumping]=function()y()C(z.Jumping)end,[Enum.HumanoidStateType.Swimming]=function()local r=math.abs(G.Velocity.Y)if r>.1 then z.Splash.Volume=math.clamp(m(r,100,350,.28,1),0,1)C(z.Splash)end y(z.Swimming)z.Swimming.Playing=true Y[z.Swimming]=true end,[Enum.HumanoidStateType.Freefall]=function()z.FreeFalling.Volume=0 y(z.FreeFalling)Y[z.FreeFalling]=true end;[Enum.HumanoidStateType.Landed]=function()y()local r=math.abs(G.Velocity.Y)if r>75 then z.Landing.Volume=math.clamp(m(r,50,100,0,1),0,1)C(z.Landing)end end;[Enum.HumanoidStateType.Running]=function()y(z.Running)z.Running.Playing=true Y[z.Running]=true end;[Enum.HumanoidStateType.Climbing]=function()local m=z.Climbing if math.abs(G.Velocity.Y)>.1 then m.Playing=true y(m)else y()end Y[m]=true end;[Enum.HumanoidStateType.Seated]=function()y()end;[Enum.HumanoidStateType.Dead]=function()y()C(z.Died)end}local I={[z.Climbing]=function(m,C,r)C.Playing=r.Magnitude>.1 end,[z.FreeFalling]=function(m,C,r)if r.Magnitude>75 then C.Volume=math.clamp(C.Volume+.9*m,0,1)else C.Volume=0 end end;[z.Running]=function(m,C,r)C.Playing=r.Magnitude>.5 and Z.MoveDirection.Magnitude>.5 end}local U={[Enum.HumanoidStateType.RunningNoPhysics]=Enum.HumanoidStateType.Running}local H=U[Z:GetState()]or Z:GetState()local O=Z.StateChanged:Connect(function(m,C)C=U[C]or C if C~=H then local m=A[C]if m then m()end H=C end end)local X=RunService.Stepped:Connect(function(m,C)for m in pairs(Y)do local r=I[m]if r then r(C,m,G.Velocity)end end end)local K local g local f local function T()O:Disconnect()X:Disconnect()K:Disconnect()g:Disconnect()f:Disconnect()end K=Z.AncestryChanged:Connect(function(m,C)if not C then T()end end)g=G.AncestryChanged:Connect(function(m,C)if not C then T()end end)f=v.CharacterAdded:Connect(T)end local function Z(m)local function C(C)if not C.Parent then waitForFirst(C.AncestryChanged,m.CharacterAdded)end if m.Character~=C or not C.Parent then return end local r=C:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")while C:IsDescendantOf(game)and not r do waitForFirst(C.ChildAdded,C.AncestryChanged,m.CharacterAdded)r=C:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")end if m.Character~=C or not C:IsDescendantOf(game)then return end local Z=C:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")while C:IsDescendantOf(game)and not Z do waitForFirst(C.ChildAdded,C.AncestryChanged,r.AncestryChanged,m.CharacterAdded)Z=C:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")end if Z and(r:IsDescendantOf(game)and(C:IsDescendantOf(game)and m.Character==C))then v(m,r,Z)end end if m.Character then C(m.Character)end m.CharacterAdded:Connect(C)end Players.PlayerAdded:Connect(Z)for m,C in ipairs(Players:GetPlayers())do Z(C)end

local function map(x, inMin, inMax, outMin, outMax)
	return (x - inMin)*(outMax - outMin)/(inMax - inMin) + outMin
end

local function playSound(sound)
	sound.TimePosition = 0
	sound.Playing = true
end

local function shallowCopy(t)
	local out = {}
	for k, v in pairs(t) do
		out[k] = v
	end
	return out
end

local function initializeSoundSystem(player, humanoid, rootPart)
	local sounds = {}

	-- initialize sounds
	for name, props in pairs(SOUND_DATA) do
		local sound = Instance.new("Sound")
		sound.Name = name

		-- set default values
		sound.Archivable = false
		sound.EmitterSize = 5
		sound.MaxDistance = 150
		sound.Volume = 0.65

		for propName, propValue in pairs(props) do
			sound[propName] = propValue
		end

		sound.Parent = rootPart
		sounds[name] = sound
	end

	local playingLoopedSounds = {}

	local function stopPlayingLoopedSounds(except)
		for sound in pairs(shallowCopy(playingLoopedSounds)) do
			if sound ~= except then
				sound.Playing = false
				playingLoopedSounds[sound] = nil
			end
		end
	end

	-- state transition callbacks
	local stateTransitions = {
		[Enum.HumanoidStateType.FallingDown] = function()
			stopPlayingLoopedSounds()
		end,

		[Enum.HumanoidStateType.GettingUp] = function()
			stopPlayingLoopedSounds()
			playSound(sounds.GettingUp)
		end,

		[Enum.HumanoidStateType.Jumping] = function()
			stopPlayingLoopedSounds()
			playSound(sounds.Jumping)
		end,

		[Enum.HumanoidStateType.Swimming] = function()
			local verticalSpeed = math.abs(rootPart.Velocity.Y)
			if verticalSpeed > 0.1 then
				sounds.Splash.Volume = math.clamp(map(verticalSpeed, 100, 350, 0.28, 1), 0, 1)
				playSound(sounds.Splash)
			end
			stopPlayingLoopedSounds(sounds.Swimming)
			sounds.Swimming.Playing = true
			playingLoopedSounds[sounds.Swimming] = true
		end,

		[Enum.HumanoidStateType.Freefall] = function()
			sounds.FreeFalling.Volume = 0
			stopPlayingLoopedSounds(sounds.FreeFalling)
			playingLoopedSounds[sounds.FreeFalling] = true
		end,

		[Enum.HumanoidStateType.Landed] = function()
			stopPlayingLoopedSounds()
			local verticalSpeed = math.abs(rootPart.Velocity.Y)
			if verticalSpeed > 75 then
				sounds.Landing.Volume = math.clamp(map(verticalSpeed, 50, 100, 0, 1), 0, 1)
				playSound(sounds.Landing)
			end
		end,

		[Enum.HumanoidStateType.Running] = function()
			stopPlayingLoopedSounds(sounds.Running)
			sounds.Running.Playing = true
			playingLoopedSounds[sounds.Running] = true
		end,

		[Enum.HumanoidStateType.Climbing] = function()
			local sound = sounds.Climbing
			if math.abs(rootPart.Velocity.Y) > 0.1 then
				sound.Playing = true
				stopPlayingLoopedSounds(sound)
			else
				stopPlayingLoopedSounds()
			end
			playingLoopedSounds[sound] = true
		end,

		[Enum.HumanoidStateType.Seated] = function()
			stopPlayingLoopedSounds()
		end,

		[Enum.HumanoidStateType.Dead] = function()
			stopPlayingLoopedSounds()
			playSound(sounds.Died)
		end,
	}

	-- updaters for looped sounds
	local loopedSoundUpdaters = {
		[sounds.Climbing] = function(dt, sound, vel)
			sound.Playing = vel.Magnitude > 0.1
		end,

		[sounds.FreeFalling] = function(dt, sound, vel)
			if vel.Magnitude > 75 then
				sound.Volume = math.clamp(sound.Volume + 0.9*dt, 0, 1)
			else
				sound.Volume = 0
			end
		end,

		[sounds.Running] = function(dt, sound, vel)
			sound.Playing = vel.Magnitude > 0.5 and humanoid.MoveDirection.Magnitude > 0.5
		end,
	}

	-- state substitutions to avoid duplicating entries in the state table
	local stateRemap = {
		[Enum.HumanoidStateType.RunningNoPhysics] = Enum.HumanoidStateType.Running,
	}

	local activeState = stateRemap[humanoid:GetState()] or humanoid:GetState()

	local stateChangedConn = humanoid.StateChanged:Connect(function(_, state)
		state = stateRemap[state] or state

		if state ~= activeState then
			local transitionFunc = stateTransitions[state]

			if transitionFunc then
				transitionFunc()
			end

			activeState = state
		end
	end)

	local steppedConn = RunService.Stepped:Connect(function(_, worldDt)
		-- update looped sounds on stepped
		for sound in pairs(playingLoopedSounds) do
			local updater = loopedSoundUpdaters[sound]

			if updater then
				updater(worldDt, sound, rootPart.Velocity)
			end
		end
	end)

	local humanoidAncestryChangedConn
	local rootPartAncestryChangedConn
	local characterAddedConn

	local function terminate()
		stateChangedConn:Disconnect()
		steppedConn:Disconnect()
		humanoidAncestryChangedConn:Disconnect()
		rootPartAncestryChangedConn:Disconnect()
		characterAddedConn:Disconnect()
	end

	humanoidAncestryChangedConn = humanoid.AncestryChanged:Connect(function(_, parent)
		if not parent then
			terminate()
		end
	end)

	rootPartAncestryChangedConn = rootPart.AncestryChanged:Connect(function(_, parent)
		if not parent then
			terminate()
		end
	end)

	characterAddedConn = player.CharacterAdded:Connect(terminate)
end

local function playerAdded(player)
	local function characterAdded(character)
		-- Avoiding memory leaks in the face of Character/Humanoid/RootPart lifetime has a few complications:
		-- * character deparenting is a Remove instead of a Destroy, so signals are not cleaned up automatically.
		-- ** must use a waitForFirst on everything and listen for hierarchy changes.
		-- * the character might not be in the dm by the time CharacterAdded fires
		-- ** constantly check consistency with player.Character and abort if CharacterAdded is fired again
		-- * Humanoid may not exist immediately, and by the time it's inserted the character might be deparented.
		-- * RootPart probably won't exist immediately.
		-- ** by the time RootPart is inserted and Humanoid.RootPart is set, the character or the humanoid might be deparented.

		if not character.Parent then
			waitForFirst(character.AncestryChanged, player.CharacterAdded)
		end

		if player.Character ~= character or not character.Parent then
			return
		end

		local humanoid = character:FindFirstChildOfClass("Humanoid")
		while character:IsDescendantOf(game) and not humanoid do
			waitForFirst(character.ChildAdded, character.AncestryChanged, player.CharacterAdded)
			humanoid = character:FindFirstChildOfClass("Humanoid")
		end

		if player.Character ~= character or not character:IsDescendantOf(game) then
			return
		end

		-- must rely on HumanoidRootPart naming because Humanoid.RootPart does not fire changed signals
		local rootPart = character:FindFirstChild("HumanoidRootPart")
		while character:IsDescendantOf(game) and not rootPart do
			waitForFirst(character.ChildAdded, character.AncestryChanged, humanoid.AncestryChanged, player.CharacterAdded)
			rootPart = character:FindFirstChild("HumanoidRootPart")
		end

		if rootPart and humanoid:IsDescendantOf(game) and character:IsDescendantOf(game) and player.Character == character then
			initializeSoundSystem(player, humanoid, rootPart)
		end
	end

	if player.Character then
		characterAdded(player.Character)
	end
	player.CharacterAdded:Connect(characterAdded)
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
	playerAdded(player)
end
