local O={}local U=game:GetService("\083\111\117\110\100\083\101\114\118\105\099\101")do local U=script.Parent.Parent Head=U:WaitForChild("\072\101\097\100")while not Humanoid do for O,U in pairs(U:GetChildren())do if U:IsA("\072\117\109\097\110\111\105\100")then Humanoid=U break end end if Humanoid then break end U.ChildAdded:wait()end O[SFX.Died]=Head:WaitForChild("\068\105\101\100")O[SFX.Running]=Head:WaitForChild("\082\117\110\110\105\110\103")O[SFX.Swimming]=Head:WaitForChild("\083\119\105\109\109\105\110\103")O[SFX.Climbing]=Head:WaitForChild("\067\108\105\109\098\105\110\103")O[SFX.Jumping]=Head:WaitForChild("\074\117\109\112\105\110\103")O[SFX.GettingUp]=Head:WaitForChild("\071\101\116\116\105\110\103\085\112")O[SFX.FreeFalling]=Head:WaitForChild("\070\114\101\101\070\097\108\108\105\110\103")O[SFX.Landing]=Head:WaitForChild("\076\097\110\100\105\110\103")O[SFX.Splash]=Head:WaitForChild("\083\112\108\097\115\104")local m=(game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")):FindFirstChild("\068\101\102\097\117\108\116\083\101\114\118\101\114\083\111\117\110\100\069\118\101\110\116")if m then m.OnClientEvent:connect(function(O,U,m)if(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\080\108\097\121\067\104\097\114\097\099\116\101\114\076\111\111\112\083\111\117\110\100\087\104\101\110\070\069")then if m and O.TimePosition~=0 then O.TimePosition=0 end if O.IsPlaying~=U then O.Playing=U end else if O.TimePosition~=0 then O.TimePosition=0 end if not O.IsPlaying then O.Playing=true end end end)end end local m=function()return game.Workspace.FilteringEnabled and U.RespectFilteringEnabled end local Z Z={YForLineGivenXAndTwoPts=function(O,U,m,Z,v)local K=(m-v)/(U-Z)local M=m-K*U return K*O+M end;Clamp=function(O,U,m)return math.min(m,math.max(U,O))end,HorizontalSpeed=function(O)local U=O.Velocity+Vector3.new(0,-O.Velocity.Y,0)return U.magnitude end,VerticalSpeed=function(O)return math.abs(O.Velocity.Y)end;Play=function(O)if m()then O.CharacterSoundEvent:FireServer(true,true)end if O.TimePosition~=0 then O.TimePosition=0 end if not O.IsPlaying then O.Playing=true end end;Pause=function(O)if(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\080\108\097\121\067\104\097\114\097\099\116\101\114\076\111\111\112\083\111\117\110\100\087\104\101\110\070\069")and m()then O.CharacterSoundEvent:FireServer(false,false)end if O.IsPlaying then O.Playing=false end end,Resume=function(O)if(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\080\108\097\121\067\104\097\114\097\099\116\101\114\076\111\111\112\083\111\117\110\100\087\104\101\110\070\069")and m()then O.CharacterSoundEvent:FireServer(true,false)end if not O.IsPlaying then O.Playing=true end end;Stop=function(O)if(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\080\108\097\121\067\104\097\114\097\099\116\101\114\076\111\111\112\083\111\117\110\100\087\104\101\110\070\069")and m()then O.CharacterSoundEvent:FireServer(false,true)end if O.IsPlaying then O.Playing=false end if O.TimePosition~=0 then O.TimePosition=0 end end}do local U={}local m=nil local v=0 function setSoundInPlayingLoopedSounds(O)for m=1,#U,1 do if U[m]==O then return end end table.insert(U,O)end function stopPlayingLoopedSoundsExcept(O)for m=#U,1,-1 do if U[m]~=O then Z.Pause(U[m])table.remove(U,m)end end end local K={[Enum.HumanoidStateType.Dead]=function()stopPlayingLoopedSoundsExcept()local U=O[SFX.Died]Z.Play(U)end;[Enum.HumanoidStateType.RunningNoPhysics]=function(O)stateUpdated(Enum.HumanoidStateType.Running,O)end,[Enum.HumanoidStateType.Running]=function(U)local K=O[SFX.Running]stopPlayingLoopedSoundsExcept(K)if useUpdatedLocalSoundFlag and(m==Enum.HumanoidStateType.Freefall and v>.1)then local U=math.min(1.0,math.max(.0,(v-50)/110))local m=O[SFX.FreeFalling]m.Volume=U Z.Play(m)v=0 end if useUpdatedLocalSoundFlag then if U~=nil and U>.5 then Z.Resume(K)setSoundInPlayingLoopedSounds(K)elseif U~=nil then stopPlayingLoopedSoundsExcept()end else if Z.HorizontalSpeed(Head)>.5 then Z.Resume(K)setSoundInPlayingLoopedSounds(K)else stopPlayingLoopedSoundsExcept()end end end;[Enum.HumanoidStateType.Swimming]=function(U)local v if useUpdatedLocalSoundFlag then v=U else v=Z.VerticalSpeed(Head)end if m~=Enum.HumanoidStateType.Swimming and v>.1 then local U=O[SFX.Splash]U.Volume=Z.Clamp(Z.YForLineGivenXAndTwoPts(Z.VerticalSpeed(Head),100,.28,350,1),0,1)Z.Play(U)end do local U=O[SFX.Swimming]stopPlayingLoopedSoundsExcept(U)Z.Resume(U)setSoundInPlayingLoopedSounds(U)end end;[Enum.HumanoidStateType.Climbing]=function(U)local m=O[SFX.Climbing]if useUpdatedLocalSoundFlag then if U~=nil and math.abs(U)>.1 then Z.Resume(m)stopPlayingLoopedSoundsExcept(m)else Z.Pause(m)stopPlayingLoopedSoundsExcept(m)end else if Z.VerticalSpeed(Head)>.1 then Z.Resume(m)stopPlayingLoopedSoundsExcept(m)else stopPlayingLoopedSoundsExcept()end end setSoundInPlayingLoopedSounds(m)end;[Enum.HumanoidStateType.Jumping]=function()if m==Enum.HumanoidStateType.Jumping then return end stopPlayingLoopedSoundsExcept()local U=O[SFX.Jumping]Z.Play(U)end,[Enum.HumanoidStateType.GettingUp]=function()stopPlayingLoopedSoundsExcept()local U=O[SFX.GettingUp]Z.Play(U)end,[Enum.HumanoidStateType.Freefall]=function()if m==Enum.HumanoidStateType.Freefall then return end local U=O[SFX.FreeFalling]U.Volume=0 stopPlayingLoopedSoundsExcept()v=math.max(v,math.abs(Head.Velocity.y))end,[Enum.HumanoidStateType.FallingDown]=function()stopPlayingLoopedSoundsExcept()end;[Enum.HumanoidStateType.Landed]=function()stopPlayingLoopedSoundsExcept()if Z.VerticalSpeed(Head)>75 then local U=O[SFX.Landing]U.Volume=Z.Clamp(Z.YForLineGivenXAndTwoPts(Z.VerticalSpeed(Head),50,0,100,1),0,1)Z.Play(U)end end,[Enum.HumanoidStateType.Seated]=function()stopPlayingLoopedSoundsExcept()end}function stateUpdated(O,U)if K[O]~=nil then if useUpdatedLocalSoundFlag and(O==Enum.HumanoidStateType.Running or O==Enum.HumanoidStateType.Climbing or O==Enum.HumanoidStateType.Swimming or O==Enum.HumanoidStateType.RunningNoPhysics)then K[O](U)else K[O]()end end m=O end Humanoid.Died:connect(function()stateUpdated(Enum.HumanoidStateType.Dead)end)Humanoid.Running:connect(function(O)stateUpdated(Enum.HumanoidStateType.Running,O)end)Humanoid.Swimming:connect(function(O)stateUpdated(Enum.HumanoidStateType.Swimming,O)end)Humanoid.Climbing:connect(function(O)stateUpdated(Enum.HumanoidStateType.Climbing,O)end)Humanoid.Jumping:connect(function()stateUpdated(Enum.HumanoidStateType.Jumping)end)Humanoid.GettingUp:connect(function()stateUpdated(Enum.HumanoidStateType.GettingUp)end)Humanoid.FreeFalling:connect(function()stateUpdated(Enum.HumanoidStateType.Freefall)end)Humanoid.FallingDown:connect(function()stateUpdated(Enum.HumanoidStateType.FallingDown)end)Humanoid.StateChanged:connect(function(O,U)stateUpdated(U)end)function onUpdate(U,v)local K=U/v do local U=O[SFX.FreeFalling]if m==Enum.HumanoidStateType.Freefall then if Head.Velocity.Y<0 and Z.VerticalSpeed(Head)>75 then Z.Resume(U)local O=1.1 local m=v/O U.Volume=Z.Clamp(U.Volume+m*K,0,1)else U.Volume=0 end else Z.Pause(U)end end do local U=O[SFX.Running]if m==Enum.HumanoidStateType.Running then if Z.HorizontalSpeed(Head)<.5 then Z.Pause(U)end end end end local M=tick()local g=.25 while true do onUpdate(tick()-M,g)M=tick()wait(g)end end

local Sounds = {}
local SoundService = game:GetService("SoundService")

do
	local Figure = script.Parent.Parent
	Head = Figure:WaitForChild("Head")
	while not Humanoid do
		for _,NewHumanoid in pairs(Figure:GetChildren()) do
			if NewHumanoid:IsA("Humanoid") then
				Humanoid = NewHumanoid
				break
			end
		end
		if Humanoid then break end
		Figure.ChildAdded:wait()
	end
	Sounds[SFX.Died] = 			Head:WaitForChild("Died")
	Sounds[SFX.Running] = 		Head:WaitForChild("Running")
	Sounds[SFX.Swimming] = 	Head:WaitForChild("Swimming")
	Sounds[SFX.Climbing] = 	Head:WaitForChild("Climbing")
	Sounds[SFX.Jumping] = 		Head:WaitForChild("Jumping")
	Sounds[SFX.GettingUp] = 	Head:WaitForChild("GettingUp")
	Sounds[SFX.FreeFalling] = 	Head:WaitForChild("FreeFalling")
	Sounds[SFX.Landing] = 		Head:WaitForChild("Landing")
	Sounds[SFX.Splash] = 		Head:WaitForChild("Splash")

	local DefaultServerSoundEvent = game:GetService("ReplicatedStorage"):FindFirstChild("DefaultServerSoundEvent")
	if DefaultServerSoundEvent then
		DefaultServerSoundEvent.OnClientEvent:connect(function(sound, playing, resetPosition)
			if UserSettings():IsUserFeatureEnabled("UserPlayCharacterLoopSoundWhenFE") then
				if resetPosition and sound.TimePosition ~= 0 then
					sound.TimePosition = 0
				end
				if sound.IsPlaying ~= playing then
					sound.Playing = playing
				end
			else
				if sound.TimePosition ~= 0 then
					sound.TimePosition = 0
				end
				if not sound.IsPlaying then
					sound.Playing = true
				end
			end
		end)
	end
end

local IsSoundFilteringEnabled = function()
	return game.Workspace.FilteringEnabled and SoundService.RespectFilteringEnabled
end

local Util
Util = {

	--Define linear relationship between (pt1x,pt2x) and (pt2x,pt2y). Evaluate this at x.
	YForLineGivenXAndTwoPts = function(x,pt1x,pt1y,pt2x,pt2y)
		--(y - y1)/(x - x1) = m
		local m = (pt1y - pt2y) / (pt1x - pt2x)
		--float b = pt1.y - m * pt1.x;
		local b = (pt1y - m * pt1x)
		return m * x + b
	end;

	--Clamps the value of "val" between the "min" and "max"
	Clamp = function(val,min,max)
		return math.min(max,math.max(min,val))	
	end;

	--Gets the horizontal (x,z) velocity magnitude of the given part
	HorizontalSpeed = function(Head)
		local hVel = Head.Velocity + Vector3.new(0,-Head.Velocity.Y,0)
		return hVel.magnitude	
	end;

	--Gets the vertical (y) velocity magnitude of the given part
	VerticalSpeed = function(Head)
		return math.abs(Head.Velocity.Y)
	end;

	--Setting Playing/TimePosition values directly result in less network traffic than Play/Pause/Resume/Stop
	--If these properties are enabled, use them.
	Play = function(sound)		
		if IsSoundFilteringEnabled() then
			sound.CharacterSoundEvent:FireServer(true, true)
		end
		if sound.TimePosition ~= 0 then
			sound.TimePosition = 0
		end
		if not sound.IsPlaying then
			sound.Playing = true
		end
	end;

	Pause = function(sound)
		if UserSettings():IsUserFeatureEnabled("UserPlayCharacterLoopSoundWhenFE") and IsSoundFilteringEnabled() then
			sound.CharacterSoundEvent:FireServer(false, false)
		end
		if sound.IsPlaying then
			sound.Playing = false
		end
	end;

	Resume = function(sound)
		if UserSettings():IsUserFeatureEnabled("UserPlayCharacterLoopSoundWhenFE") and IsSoundFilteringEnabled() then
			sound.CharacterSoundEvent:FireServer(true, false)
		end
		if not sound.IsPlaying then
			sound.Playing = true
		end
	end;

	Stop = function(sound)
		if UserSettings():IsUserFeatureEnabled("UserPlayCharacterLoopSoundWhenFE") and IsSoundFilteringEnabled() then
			sound.CharacterSoundEvent:FireServer(false, true)
		end
		if sound.IsPlaying then
			sound.Playing = false
		end
		if sound.TimePosition ~= 0 then
			sound.TimePosition = 0
		end
	end;
}

do
	-- List of all active Looped sounds
	local playingLoopedSounds = {}

	-- Last seen Enum.HumanoidStateType
	local activeState = nil

	local fallSpeed = 0

	-- Verify and set that "sound" is in "playingLoopedSounds".
	function setSoundInPlayingLoopedSounds(sound)
		for i=1, #playingLoopedSounds do
			if playingLoopedSounds[i] == sound then
				return
			end
		end	
		table.insert(playingLoopedSounds,sound)
	end

	-- Stop all active looped sounds except parameter "except". If "except" is not passed, all looped sounds will be stopped.
	function stopPlayingLoopedSoundsExcept(except)
		for i=#playingLoopedSounds,1,-1 do
			if playingLoopedSounds[i] ~= except then
				Util.Pause(playingLoopedSounds[i])			
				table.remove(playingLoopedSounds,i)	
			end
		end
	end

	-- Table of Enum.HumanoidStateType to handling function
	local stateUpdateHandler = {
		[Enum.HumanoidStateType.Dead] = function()
			stopPlayingLoopedSoundsExcept()
			local sound = Sounds[SFX.Died]
			Util.Play(sound)
		end;

		[Enum.HumanoidStateType.RunningNoPhysics] = function(speed)
			stateUpdated(Enum.HumanoidStateType.Running, speed)
		end;

		[Enum.HumanoidStateType.Running] = function(speed)	
			local sound = Sounds[SFX.Running]
			stopPlayingLoopedSoundsExcept(sound)

			if(useUpdatedLocalSoundFlag and activeState == Enum.HumanoidStateType.Freefall and fallSpeed > 0.1) then
				-- Play a landing sound if the character dropped from a large distance
				local vol = math.min(1.0, math.max(0.0, (fallSpeed - 50) / 110))
				local freeFallSound = Sounds[SFX.FreeFalling]
				freeFallSound.Volume = vol
				Util.Play(freeFallSound)
				fallSpeed = 0
			end
			if useUpdatedLocalSoundFlag then
				if speed ~= nil and speed > 0.5 then
					Util.Resume(sound)
					setSoundInPlayingLoopedSounds(sound)
				elseif speed ~= nil then
					stopPlayingLoopedSoundsExcept()
				end
			else
				if Util.HorizontalSpeed(Head) > 0.5 then
					Util.Resume(sound)
					setSoundInPlayingLoopedSounds(sound)
				else
					stopPlayingLoopedSoundsExcept()
				end
			end
		end;

		[Enum.HumanoidStateType.Swimming] = function(speed)
		local threshold
		if useUpdatedLocalSoundFlag then threshold = speed else threshold = Util.VerticalSpeed(Head) end
			if activeState ~= Enum.HumanoidStateType.Swimming and threshold > 0.1 then
				local splashSound = Sounds[SFX.Splash]
				splashSound.Volume = Util.Clamp(
					Util.YForLineGivenXAndTwoPts(
						Util.VerticalSpeed(Head), 
						100, 0.28, 
						350, 1),
					0,1)
				Util.Play(splashSound)
			end

			do
				local sound = Sounds[SFX.Swimming]
				stopPlayingLoopedSoundsExcept(sound)
				Util.Resume(sound)
				setSoundInPlayingLoopedSounds(sound)
			end
		end;

		[Enum.HumanoidStateType.Climbing] = function(speed)
			local sound = Sounds[SFX.Climbing]
			if useUpdatedLocalSoundFlag then
				if speed ~= nil and math.abs(speed) > 0.1 then
					Util.Resume(sound)
					stopPlayingLoopedSoundsExcept(sound)
				else
					Util.Pause(sound)
					stopPlayingLoopedSoundsExcept(sound)
				end		
			else
				if Util.VerticalSpeed(Head) > 0.1 then
					Util.Resume(sound)
					stopPlayingLoopedSoundsExcept(sound)
				else
					stopPlayingLoopedSoundsExcept()
				end
			end

			setSoundInPlayingLoopedSounds(sound)
		end;

		[Enum.HumanoidStateType.Jumping] = function()
			if activeState == Enum.HumanoidStateType.Jumping then
				return
			end		
			stopPlayingLoopedSoundsExcept()
			local sound = Sounds[SFX.Jumping]
			Util.Play(sound)
		end;

		[Enum.HumanoidStateType.GettingUp] = function()
			stopPlayingLoopedSoundsExcept()
			local sound = Sounds[SFX.GettingUp]
			Util.Play(sound)
		end;

		[Enum.HumanoidStateType.Freefall] = function()
			if activeState == Enum.HumanoidStateType.Freefall then
				return
			end
			local sound = Sounds[SFX.FreeFalling]
			sound.Volume = 0
			stopPlayingLoopedSoundsExcept()

			fallSpeed = math.max(fallSpeed, math.abs(Head.Velocity.y))
		end;

		[Enum.HumanoidStateType.FallingDown] = function()
			stopPlayingLoopedSoundsExcept()
		end;

		[Enum.HumanoidStateType.Landed] = function()
			stopPlayingLoopedSoundsExcept()
			if Util.VerticalSpeed(Head) > 75 then
				local landingSound = Sounds[SFX.Landing]
				landingSound.Volume = Util.Clamp(
					Util.YForLineGivenXAndTwoPts(
						Util.VerticalSpeed(Head), 
						50, 0, 
						100, 1),
					0,1)
				Util.Play(landingSound)			
			end
		end;

		[Enum.HumanoidStateType.Seated] = function()
			stopPlayingLoopedSoundsExcept()
		end;	
	}

	

	-- Handle state event fired or OnChange fired
	function stateUpdated(state, speed)
		if stateUpdateHandler[state] ~= nil then
			if useUpdatedLocalSoundFlag and (state == Enum.HumanoidStateType.Running 
				or state == Enum.HumanoidStateType.Climbing
				or state == Enum.HumanoidStateType.Swimming
				or state == Enum.HumanoidStateType.RunningNoPhysics) then
				stateUpdateHandler[state](speed)
			else
				stateUpdateHandler[state]()
			end
		end
		activeState = state
	end

	Humanoid.Died:connect(			function() stateUpdated(Enum.HumanoidStateType.Dead) 			end)
	Humanoid.Running:connect(		function(speed) stateUpdated(Enum.HumanoidStateType.Running, speed) end)
	Humanoid.Swimming:connect(		function(speed) stateUpdated(Enum.HumanoidStateType.Swimming, speed) end)
	Humanoid.Climbing:connect(		function(speed) stateUpdated(Enum.HumanoidStateType.Climbing, speed) end)
	Humanoid.Jumping:connect(		function() stateUpdated(Enum.HumanoidStateType.Jumping) 		end)
	Humanoid.GettingUp:connect(		function() stateUpdated(Enum.HumanoidStateType.GettingUp) 		end)
	Humanoid.FreeFalling:connect(	function() stateUpdated(Enum.HumanoidStateType.Freefall) 		end)
	Humanoid.FallingDown:connect(	function() stateUpdated(Enum.HumanoidStateType.FallingDown) 	end)

	

	-- required for proper handling of Landed event

	Humanoid.StateChanged:connect(function(old, new)
		stateUpdated(new)
	end)

	

	function onUpdate(stepDeltaSeconds, tickSpeedSeconds)
		local stepScale = stepDeltaSeconds / tickSpeedSeconds
		do
			local sound = Sounds[SFX.FreeFalling]
			if activeState == Enum.HumanoidStateType.Freefall then
				if Head.Velocity.Y < 0 and Util.VerticalSpeed(Head) > 75 then
					Util.Resume(sound)

					--Volume takes 1.1 seconds to go from volume 0 to 1
					local ANIMATION_LENGTH_SECONDS = 1.1

					local normalizedIncrement = tickSpeedSeconds / ANIMATION_LENGTH_SECONDS									
					sound.Volume = Util.Clamp(sound.Volume + normalizedIncrement * stepScale, 0, 1)
				else
					sound.Volume = 0
				end			
			else
				Util.Pause(sound)
			end
		end

		do
			local sound = Sounds[SFX.Running]
			if activeState == Enum.HumanoidStateType.Running then
				if Util.HorizontalSpeed(Head) < 0.5 then
					Util.Pause(sound)
				end
			end
		end		
	end

	
	local lastTick = tick()
	local TICK_SPEED_SECONDS = 0.25
	while true do
		onUpdate(tick() - lastTick,TICK_SPEED_SECONDS)
		lastTick = tick()
		wait(TICK_SPEED_SECONDS)
	end

end
