What is a more efficient way to change the player's walk sound pitch depending on their speed?

I made this local script, where it sucessfully changed the player’s walksound to be my selected id, but when changing the pitch depending on the player’s speed it doesn’t achieve anything.

local sound = script.Parent.HumanoidRootPart:WaitForChild("Running")
local humanoid = script.Parent:FindFirstChild("Humanoid")
sound.SoundId = ("rbxassetid://9083849830")
sound.PlaybackSpeed = 1

while true do
if humanoid.WalkSpeed == 25 then
	    sound.PlaybackSpeed = 1.2
	end
	
if humanoid.WalkSpeed == 14 then
		sound.PlaybackSpeed = 1
	end
end

I’m not sure if you’re asking to change the sound speed rather than the pitch. You can’t set a sounds pitch but you can set its speed. Here’s some code for changing the sound depending on their walk speed

local humanoid = script.Parent:FindFirstChild("Humanoid")
sound.SoundId = ("rbxassetid://9083849830")
sound.PlaybackSpeed = 1

while true do
sound.PlaybackSpeed = 16 / humanoid.WalkSpeed
task.wait(0.05)
end

Also be sure that your script is a local script since the object doesn’t exist on the server

i meant sound speed, woops
However, when i try your script whenever the player’s speed is set to my target speed (of 25) the sound slows down rather than speeding up.

Swap it around to humanoid.WalkSpeed / 16 instead.

logical, i did not think of that LOL but it now works fine, thanks!

1 Like

You could also change the while true loop to a changed event for extra optimization :smile:

local sound = script.Parent.HumanoidRootPart:WaitForChild("Running")
local humanoid = script.Parent:FindFirstChild("Humanoid")
sound.SoundId = ("rbxassetid://9083849830")
sound.PlaybackSpeed = 1

humanoid.Changed:Connect(function(property)
	if property == "WalkSpeed" then
		if humanoid.WalkSpeed == 25 then
			sound.PlaybackSpeed = 1.2
		end

		if humanoid.WalkSpeed == 14 then
			sound.PlaybackSpeed = 1
		end
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.