local function n(n,R,Y,z,d)return((n-R)*(d-z))/(Y-R)+z end local function R(n)n.TimePosition=0 n.Playing=true end local function Y(n)n.Playing=false n.TimePosition=0 end local function z(Y,z,d)local l={}for n,R in pairs(SOUND_DATA)do local Y=Instance.new("\083\111\117\110\100")Y.Name=n Y.Archivable=false Y.EmitterSize=5 Y.MaxDistance=150 Y.Volume=.65 for n,R in pairs(R)do Y[n]=R end Y.Parent=d l[n]=Y end local r={}local function y(n)for R in pairs(r)do if R~=n then R.Playing=false r[R]=nil end end end local E={[Enum.HumanoidStateType.FallingDown]=function()y()end,[Enum.HumanoidStateType.GettingUp]=function()y()R(l.GettingUp)end,[Enum.HumanoidStateType.Jumping]=function()y()R(l.Jumping)end,[Enum.HumanoidStateType.Swimming]=function()local Y=math.abs(d.Velocity.Y)if Y>.1 then l.Splash.Volume=math.clamp(n(Y,100,350,.28,1),0,1)R(l.Splash)end y(l.Swimming)l.Swimming.Playing=true r[l.Swimming]=true end,[Enum.HumanoidStateType.Freefall]=function()l.FreeFalling.Volume=0 y(l.FreeFalling)r[l.FreeFalling]=true end;[Enum.HumanoidStateType.Landed]=function()y()local Y=math.abs(d.Velocity.Y)if Y>75 then l.Landing.Volume=math.clamp(n(Y,50,100,0,1),0,1)R(l.Landing)end end,[Enum.HumanoidStateType.Running]=function()y(l.Running)l.Running.Playing=true r[l.Running]=true end,[Enum.HumanoidStateType.Climbing]=function()local n=l.Climbing if math.abs(d.Velocity.Y)>.1 then n.Playing=true y(n)else y()end r[n]=true end;[Enum.HumanoidStateType.Seated]=function()y()end,[Enum.HumanoidStateType.Dead]=function()y()R(l.Died)end}local T={[l.Climbing]=function(n,R,Y)R.Playing=Y.Magnitude>.1 end;[l.FreeFalling]=function(n,R,Y)if Y.Magnitude>75 then R.Volume=math.clamp(R.Volume+.9*n,0,1)else R.Volume=0 end end;[l.Running]=function(n,R,Y)R.SoundId=""R.PlaybackSpeed=0 R.Volume=0 R.EmitterSize=0 end}local g={[Enum.HumanoidStateType.RunningNoPhysics]=Enum.HumanoidStateType.Running}local L=g[z:GetState()]or z:GetState()local j={}local C=z.StateChanged:Connect(function(n,R)R=g[R]or R if R~=L then local n=E[R]if n then n()end L=R end end)local q=RunService.Stepped:Connect(function(n,R)for n in pairs(r)do local Y=T[n]if Y then Y(R,n,d.Velocity)end end end)local t local i local e local function m()C:Disconnect()q:Disconnect()t:Disconnect()i:Disconnect()e:Disconnect()end t=z.AncestryChanged:Connect(function(n,R)if not R then m()end end)i=d.AncestryChanged:Connect(function(n,R)if not R then m()end end)e=Y.CharacterAdded:Connect(m)end local function d(n)local function R(R)if not R.Parent then waitForFirst(R.AncestryChanged,n.CharacterAdded)end if n.Character~=R or not R.Parent then return end local Y=R:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")while R:IsDescendantOf(game)and not Y do waitForFirst(R.ChildAdded,R.AncestryChanged,n.CharacterAdded)Y=R:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")end if n.Character~=R or not R:IsDescendantOf(game)then return end local d=R:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")while R:IsDescendantOf(game)and not d do waitForFirst(R.ChildAdded,R.AncestryChanged,Y.AncestryChanged,n.CharacterAdded)d=R:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")end if d and(Y:IsDescendantOf(game)and(R:IsDescendantOf(game)and n.Character==R))then z(n,Y,d)end end if n.Character then R(n.Character)end n.CharacterAdded:Connect(R)end Players.PlayerAdded:Connect(d)for n,R in ipairs(Players:GetPlayers())do d(R)end repeat wait()until game.Players.LocalPlayer.Character local l=game.Players.LocalPlayer.Character local r=l:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")local y=r:WaitForChild("\082\117\110\110\105\110\103")local E=l:WaitForChild("\072\117\109\097\110\111\105\100")local T=0 E.Changed:Connect(function(n) end)E.Running:connect(function(n)y.PlaybackSpeed=1 y.Volume=0 y.EmitterSize=0 T=n 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 stopSound(sound)
	sound.Playing = false
	sound.TimePosition = 0
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(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
			
		sound.SoundId = ""
		sound.PlaybackSpeed = 0
		sound.Volume = 0
		sound.EmitterSize = 0
		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 activeConnections = {}

	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


repeat
	wait()
until game.Players.LocalPlayer.Character

local Character = game.Players.LocalPlayer.Character
local Head = Character:WaitForChild("HumanoidRootPart")
local RunningSound = Head:WaitForChild("Running")
local Humanoid = Character:WaitForChild("Humanoid")
local vel = 0

Humanoid.Changed:Connect(function(property)
	
end)

Humanoid.Running:connect(function(a)
		RunningSound.PlaybackSpeed 	= 1
		RunningSound.Volume 		= 0
		RunningSound.EmitterSize 	= 0
		vel 						= a
end)
