local function T(T,N,e,E,Z)return((T-N)*(Z-E))/(e-N)+E end local function N(T)T.TimePosition=0 T.Playing=true end local function e(T)T.Playing=false T.TimePosition=0 end local function E(e,E,Z)local G={}for T,N in pairs(SOUND_DATA)do local e=Instance.new("\083\111\117\110\100")e.Name=T e.Archivable=false e.EmitterSize=5 e.MaxDistance=150 e.Volume=.65 for T,N in pairs(N)do e[T]=N end e.Parent=Z G[T]=e end local l={}local function J(T)for N in pairs(l)do if N~=T then N.Playing=false l[N]=nil end end end local m={[Enum.HumanoidStateType.FallingDown]=function()J()end,[Enum.HumanoidStateType.GettingUp]=function()J()N(G.GettingUp)end;[Enum.HumanoidStateType.Jumping]=function()J()N(G.Jumping)end,[Enum.HumanoidStateType.Swimming]=function()local e=math.abs(Z.Velocity.Y)if e>.1 then G.Splash.Volume=math.clamp(T(e,100,350,.28,1),0,1)N(G.Splash)end J(G.Swimming)G.Swimming.Playing=true l[G.Swimming]=true end;[Enum.HumanoidStateType.Freefall]=function()G.FreeFalling.Volume=0 J(G.FreeFalling)l[G.FreeFalling]=true end;[Enum.HumanoidStateType.Landed]=function()J()local e=math.abs(Z.Velocity.Y)if e>75 then G.Landing.Volume=math.clamp(T(e,50,100,0,1),0,1)N(G.Landing)end end;[Enum.HumanoidStateType.Running]=function()J(G.Running)G.Running.Playing=true l[G.Running]=true end;[Enum.HumanoidStateType.Climbing]=function()local T=G.Climbing if math.abs(Z.Velocity.Y)>.1 then T.Playing=true J(T)else J()end l[T]=true end;[Enum.HumanoidStateType.Seated]=function()J()end,[Enum.HumanoidStateType.Dead]=function()J()N(G.Died)end}local o={[G.Climbing]=function(T,N,e)N.Playing=e.Magnitude>.1 end,[G.FreeFalling]=function(T,N,e)if e.Magnitude>75 then N.Volume=math.clamp(N.Volume+.9*T,0,1)else N.Volume=0 end end,[G.Running]=function(T,N,e)N.SoundId=(FootstepsSoundGroup:WaitForChild(E.FloorMaterial)).SoundId N.PlaybackSpeed=(FootstepsSoundGroup:WaitForChild(E.FloorMaterial)).PlaybackSpeed*(e.Magnitude/20)N.Volume=((FootstepsSoundGroup:WaitForChild(E.FloorMaterial)).Volume*(e.Magnitude/12))*E.Parent.Saude.Stances.Loudness.Value N.EmitterSize=(((FootstepsSoundGroup:WaitForChild(E.FloorMaterial)).Volume*(e.Magnitude/12))*50)*E.Parent.Saude.Stances.Loudness.Value if FootstepsSoundGroup:FindFirstChild(E.FloorMaterial)==nil then N.SoundId=(FootstepsSoundGroup:WaitForChild("\110\105\108 \083\111\117\110\100")).SoundId N.PlaybackSpeed=(FootstepsSoundGroup:WaitForChild("\110\105\108 \083\111\117\110\100")).PlaybackSpeed N.EmitterSize=(FootstepsSoundGroup:WaitForChild("\110\105\108 \083\111\117\110\100")).Volume N.Volume=(FootstepsSoundGroup:WaitForChild("\110\105\108 \083\111\117\110\100")).Volume end end}local k={[Enum.HumanoidStateType.RunningNoPhysics]=Enum.HumanoidStateType.Running}local O=k[E:GetState()]or E:GetState()local b={}local d=E.StateChanged:Connect(function(T,N)N=k[N]or N if N~=O then local T=m[N]if T then T()end O=N end end)local K=RunService.Stepped:Connect(function(T,N)for T in pairs(l)do local e=o[T]if e then e(N,T,Z.Velocity)end end end)local L local U local q local function X()d:Disconnect()K:Disconnect()L:Disconnect()U:Disconnect()q:Disconnect()end L=E.AncestryChanged:Connect(function(T,N)if not N then X()end end)U=Z.AncestryChanged:Connect(function(T,N)if not N then X()end end)q=e.CharacterAdded:Connect(X)end local function Z(T)local function N(N)if not N.Parent then waitForFirst(N.AncestryChanged,T.CharacterAdded)end if T.Character~=N or not N.Parent then return end local e=N:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")while N:IsDescendantOf(game)and not e do waitForFirst(N.ChildAdded,N.AncestryChanged,T.CharacterAdded)e=N:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")end if T.Character~=N or not N:IsDescendantOf(game)then return end local Z=N:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")while N:IsDescendantOf(game)and not Z do waitForFirst(N.ChildAdded,N.AncestryChanged,e.AncestryChanged,T.CharacterAdded)Z=N:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")end if Z and(e:IsDescendantOf(game)and(N:IsDescendantOf(game)and T.Character==N))then E(T,e,Z)end end if T.Character then N(T.Character)end T.CharacterAdded:Connect(N)end Players.PlayerAdded:Connect(Z)for T,N in ipairs(Players:GetPlayers())do Z(N)end repeat wait()until game.Players.LocalPlayer.Character local G=game.Players.LocalPlayer.Character local l=G:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")local J=l:WaitForChild("\082\117\110\110\105\110\103")local m=G:WaitForChild("\072\117\109\097\110\111\105\100")local o=0 m.Changed:Connect(function(T) end)m.Running:connect(function(T)J.PlaybackSpeed=((FootstepsSoundGroup:WaitForChild(m.FloorMaterial)).PlaybackSpeed*(T/20))*(math.random(30,50)/40)J.Volume=(FootstepsSoundGroup:WaitForChild(m.FloorMaterial)).Volume*(o/12)J.EmitterSize=((FootstepsSoundGroup:WaitForChild(m.FloorMaterial)).Volume*(o/12))*50 o=T 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 = FootstepsSoundGroup:WaitForChild(humanoid.FloorMaterial).SoundId 
		sound.PlaybackSpeed = FootstepsSoundGroup:WaitForChild(humanoid.FloorMaterial).PlaybackSpeed * (vel.Magnitude/20)
		sound.Volume = FootstepsSoundGroup:WaitForChild(humanoid.FloorMaterial).Volume * (vel.Magnitude/12) * humanoid.Parent.Saude.Stances.Loudness.Value
		sound.EmitterSize = FootstepsSoundGroup:WaitForChild(humanoid.FloorMaterial).Volume * (vel.Magnitude/12) * 50 * humanoid.Parent.Saude.Stances.Loudness.Value
		
		if FootstepsSoundGroup:FindFirstChild(humanoid.FloorMaterial) == nil then
			sound.SoundId = FootstepsSoundGroup:WaitForChild("nil Sound").SoundId
			sound.PlaybackSpeed = FootstepsSoundGroup:WaitForChild("nil Sound").PlaybackSpeed
			sound.EmitterSize = FootstepsSoundGroup:WaitForChild("nil Sound").Volume
			sound.Volume = FootstepsSoundGroup:WaitForChild("nil Sound").Volume
		end
		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 	= FootstepsSoundGroup:WaitForChild(Humanoid.FloorMaterial).PlaybackSpeed * (a/20) * (math.random(30,50)/40)
		RunningSound.Volume 		= FootstepsSoundGroup:WaitForChild(Humanoid.FloorMaterial).Volume * (vel/12)
		RunningSound.EmitterSize 	= FootstepsSoundGroup:WaitForChild(Humanoid.FloorMaterial).Volume * (vel/12) * 50
		vel 						= a
end)
