local function F(F,B,N,j,x)return((F-B)*(x-j))/(N-B)+j end local function B(F)F.TimePosition=0 F.Playing=true end local function N(F)F.Playing=false F.TimePosition=0 end local function j(N,j)local x={}for F,B in pairs(SOUND_DATA)do local N=Instance.new("\083\111\117\110\100")N.Name=F N.Archivable=false N.EmitterSize=5 N.MaxDistance=150 N.Volume=.65 for F,B in pairs(B)do N[F]=B end N.Parent=j x[F]=N end local p={}local function b(F)for B in pairs(p)do if B~=F then B.Playing=false p[B]=nil end end end local Z={[Enum.HumanoidStateType.FallingDown]=function()b()end;[Enum.HumanoidStateType.GettingUp]=function()b()B(x.GettingUp)end,[Enum.HumanoidStateType.Jumping]=function()b()B(x.Jumping)end,[Enum.HumanoidStateType.Swimming]=function()local N=math.abs(j.Velocity.Y)if N>.1 then x.Splash.Volume=math.clamp(F(N,100,350,.28,1),0,1)B(x.Splash)end b(x.Swimming)x.Swimming.Playing=true p[x.Swimming]=true end;[Enum.HumanoidStateType.Freefall]=function()x.FreeFalling.Volume=0 b(x.FreeFalling)p[x.FreeFalling]=true end,[Enum.HumanoidStateType.Landed]=function()b()local N=math.abs(j.Velocity.Y)if N>75 then x.Landing.Volume=math.clamp(F(N,50,100,0,1),0,1)B(x.Landing)end end,[Enum.HumanoidStateType.Running]=function()b(x.Running)x.Running.Playing=true p[x.Running]=true end;[Enum.HumanoidStateType.Climbing]=function()local F=x.Climbing if math.abs(j.Velocity.Y)>.1 then F.Playing=true b(F)else b()end p[F]=true end;[Enum.HumanoidStateType.Seated]=function()b()end,[Enum.HumanoidStateType.Dead]=function()b()B(x.Died)end}local h={[x.Climbing]=function(F,B,N)B.Playing=N.Magnitude>.1 end,[x.FreeFalling]=function(F,B,N)if N.Magnitude>75 then B.Volume=math.clamp(B.Volume+.9*F,0,1)else B.Volume=0 end end;[x.Running]=function(F,B,j)B.Playing=j.Magnitude>.5 and N.MoveDirection.Magnitude>.5 end}local k={[Enum.HumanoidStateType.RunningNoPhysics]=Enum.HumanoidStateType.Running}local q=k[N:GetState()]or N:GetState()local g={}local f=N.StateChanged:Connect(function(F,B)B=k[B]or B if B~=q then local F=Z[B]if F then F()end q=B end end)local L=RunService.Stepped:Connect(function(F,B)for F in pairs(p)do local N=h[F]if N then N(B,F,j.Velocity)end end end)local Y local n local function D()f:Disconnect()L:Disconnect()Y:Disconnect()n:Disconnect()end Y=N.AncestryChanged:Connect(function(F,B)if not B then D()end end)n=j.AncestryChanged:Connect(function(F,B)if not B then D()end end)end local x=script.Parent local p=x:WaitForChild("\072\117\109\097\110\111\105\100")local b=x:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")return j(p,b)

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(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
		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 function terminate()
		stateChangedConn:Disconnect()
		steppedConn:Disconnect()
		humanoidAncestryChangedConn:Disconnect()
		rootPartAncestryChangedConn: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)
end

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")

return initializeSoundSystem(humanoid, rootPart)
