Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does sound "Idle" continue playing?

All of the (name)=script.parent.(name) are Audios that are in a VehicleSeat with the script that contains this code. Ideally, when you touch the seat, the Start noise will play, and then if the current speed of the car is under .1 studs per second, the Idle noise will play on loop. If the car is above .1 studs per second(moving) than the Low audio will play on loop, and the Idle noise will stop....

The start sound works fine, then the Idle sound plays directly after the start sound stop, but it never stops after that point....

Gear=script.Parent.Gear
Start=script.Parent.EngineStart
Max=script.Parent.MaxEngine
High=script.Parent.HighEngine
Med=script.Parent.MediumEngine
Low=script.Parent.LowEngine
Idle=script.Parent.IdleEngine


function onTouched(hit) 
Start:Play()
wait(2.586) --Time of start noise.
Idle:Stop()
Idle.Looped = false
Low:Stop()
Low.Looped = false
if
script.Parent.Velocity.Magnitude<= .1
then Idle.Looped = true
Idle:Play()
Start:Stop()
Low:Stop()
Low.Looped = false
elseif
script.Parent.Velocity.Magnitude>= .1
then Idle.Looped = false
Idle:Stop()
Start:Stop()
Low:Play()
Low.Looped = true
end
end
script.Parent.Touched:connect(onTouched)

1 answer

I haven't test this but try to add a

while wait() do

right before the if statement at line 17

and end it under the "end" at line 31

i think the problem is that you only check once if the speed is less then .1 or more then .1

Final code:

Gear=script.Parent.Gear
Start=script.Parent.EngineStart
Max=script.Parent.MaxEngine
High=script.Parent.HighEngine
Med=script.Parent.MediumEngine
Low=script.Parent.LowEngine
Idle=script.Parent.IdleEngine


function onTouched(hit) 
Start:Play()
wait(2.586) --Time of start noise.
Idle:Stop()
Idle.Looped = false
Low:Stop()
Low.Looped = false
while wait() do
if
script.Parent.Velocity.Magnitude<= .1
then Idle.Looped = true
Idle:Play()
Start:Stop()
Low:Stop()
Low.Looped = false
elseif
script.Parent.Velocity.Magnitude>= .1
then Idle.Looped = false
Idle:Stop()
Start:Stop()
Low:Play()
Low.Looped = true
end
end 
end
script.Parent.Touched:connect(onTouched)

1 comment

0

The answer to everything is not an infinite loop........

This discussion is preserved as it was. New answers, comments, and votes are closed.