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?
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)
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)
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
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.
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.
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().
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.
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.