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