Sound only plays when I stop testing the game

So I want a sound to play when you speed up, so I made this script: (StarterGui, Local script.)

local sound = script.SpeedIncrease

while wait() do
	local wksp = game.ReplicatedStorage.Walkspeed
	if wksp.Value > 16 then
		sound:Play()
	else
		sound:Stop()
	end
end

(wksp is an int value that represents the players speed)

Whenever the players speed increases, the sound won’t play unless I stop testing the game. Is there a reason for this?

Response:

Let me know if this works;

The Code:

local sound = script.SpeedIncrease
local runservice = game:GetService('RunService')
local wksp = game.ReplicatedStorage.Walkspeed

runservice.RenderStepped:Connect(function()
    if wksp.Value > 16 then
       -- play sound
    end
end)
1 Like

Same thing happens, doesn’t work, the sound still only plays when I exit testing the game

script:

local sound = script.SpeedIncrease
local RS = game:GetService("RunService")
local wksp = game.ReplicatedStorage.Walkspeed

RS.RenderStepped:Connect(function()
	if wksp.Value > 16 then
		sound:Play()
	else
		sound:Stop()
	end
end)
1 Like

Response:

Maybe try adding the NumberValue (or IntValue) to the Player instead of keeping it in ReplicatedStorage?

1 Like

Where would I put it tho? StarterCharacterScripts, StarterPlayer, or StarterPlayerScripts,

Also here is the code to change the value of wksp(Local script in StarterCharacterScripts).

while wait() do
	local wksp = game.ReplicatedStorage.Walkspeed
	local plrs = game:GetService("Players")
	local plr = plrs.LocalPlayer
	local char = plr.Character
	local hmoid = char.Humanoid
	local WkSp = hmoid.WalkSpeed

	wksp.Value = WkSp
end
local sound = script.SpeedIncrease

spawn(function()--Test to see if the sound can play
	wait(5)
	sound:Play()
end)

local wksp = game.ReplicatedStorage.Walkspeed
wksp.Changed:Connect(function()
	print("The value changed!")
	if wksp.Value > 16 then
		print("Playing sound!")
		sound:Play()
	else
		print("Stoping sound!")
		sound:Stop()
	end
end)

You might want to consider using changed instead of a while loop. The listener function runs when the IntValue/NumberValue’s .Value property changes.

If none of the print statements print:

  • The value probably doesn’t change

If the print statements print but the sound doesn’t play:

  • Look at the parent of the sound. You might need to add: sound.Parent = game:GetService("Workspace") right after the sound variable is declared
1 Like

I spawned in and for some reason the sound started to play. The value starts at 0, not 16. Only after the character loads, then the value changes, so that might be the issue.

1 Like

Response:

Yea, you could add it to StarterCharacterScripts.

local sound = script.SpeedIncrease

--spawn(function()--Test to see if the sound can play
--	wait(5)
--	sound:Play()
--end) --REMOVED THE TEST CODE.

local wksp = game.ReplicatedStorage.Walkspeed
wksp.Changed:Connect(function()
	print("The value changed!")
	if wksp.Value > 16 then
		print("Playing sound!")
		sound:Play()
	else
		print("Stoping sound!")
		sound:Stop()
	end
end)

So the sound can play. See if the print statements above run. If they don’t, it’s probably that your Value instance doesn’t change.

1 Like

Is the sound’s PlayOnRemove property set to true?

The sound is actually music, I tested it, and it works, but it plays for like half a second. Sorry for not specifying.

You should try using the Changed event that @PseudoPerson suggested, except add in some checks to see if the sound is already playing or not. If you have the following code:

sound:Play()
wait(1)
sound:Play()

where sound refers to a sound object with audio longer than 1 second, then what you will hear is the first second played and then the sound replayed again. This is because calling Play() resets the timeposition of a sound to 0 and then plays it. This is good if you wanted to rapidly play some sound rapidly. But if you want to play an entire sound for its entire duration, then first check if it is playing or not before calling Play().

No, it is false. (This is here just to drag out the characters so I can even post this comment)

1 Like

How do I say “Is not something” like == is “It is something” what is the opposite of that?

Response:

You would do ~=.

Do you mean == versus ~= (Or perhaps not)?

have you tried…

while true do
end

?

or

for i = 1, 21 (or any number) do
end

Here is the new code:

local sound = script.SpeedIncrease

local wksp = game.ReplicatedStorage.Walkspeed
wksp.Changed:Connect(function()
	print("The value changed!")
	if wksp.Value > 16 then
		if sound.TimePosition ~= 0 then
			sound:Play()
		else
			sound:Stop()
		end
	end
end)

Doesn’t work, at all. Also, very important:

Whenever you touch a sphere to gain speed, I want music to play. I might have something that increases speed later than can ruin this whole system. This is basically a work-around to a different problem. Is there a way to make is when I touch a part, I sound plays for only that player who hit it, then, if the walk speed goes down to 16 (the walk speed decreases with tween service after the player hits the part) the music stops, but also when you touch another speed increasing part, the music doesn't replay. Probably I won't increase the player speed any other time other than this, but it's still good to have that in mind. Also looping music.

Response:

It’s as simple as adding a LocalScript into the Part with this code;

The Code:

local part = script.Parent
local sound = part.Sound

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
       sound:Play()
    end
end)

If you want audio to play such that it plays on high walkspeed, pauses on low, and resumes on high again, you can use the Sound:Pause() function to pause audio and the Sound:Resume() function to resume audio.

You should look into more of the Sound API for more information on the functions and events it contains.

Also, instead of saying if sound.TimePosition ~= 0 then you should instead say if sound.IsPlaying then because IsPlaying is a property of sounds that dictates whether or not a sound is playing.

Finally, you want to know when the player’s walkspeed changes, correct? Doesn’t that mean you should check if the player’s humanoid’s walkspeed is more than 16 rather than your number value? If this is the case, then you will also want to change the line with wksp.Changed to Humanoid.Changed so that the event is fired on the changes to the humanoid rather than the number value. Unless, of course, you already have other code that updates a player’s walkspeed whenever wksp changes.