Working on an APC remake from Half Life. I have the siren currently set to a click detector (though if someone could come up with a way to do it using a key once in the driver seat that'd be much better).
Anyway, right now the script for the siren is just
local on = false script.Parent.ClickDetector.MouseClick:connect(function() if on then script.Parent.Siren:Stop() on = false else script.Parent.Siren:Play() on = true end end)
A way to make it slowly fade in and out on click would be appreciated. (As you can see, my scripting knowledge is very basic)
A way to do this is to use the numeric for loop. What to use it on? The volume of course.
local on = false local siren = script.Parent.Siren local vol = siren.Volume script.Parent.ClickDetector.MouseClick:connect(function() on = not on if on then siren:Play() for i = 0.1, vol, .1 do siren.Volume = i wait(1) end siren.Volume = 1 else for i = vol, 0.1, .1 do siren.Volume = i wait(1) end siren.Volume = 0 siren:Stop() end end)
As Dueling suggests, you need to use a numeric for loop, although his code won't work! Why you might ask? ...he doesn't yield (wait)! It immediately goes from 0 to 1, even though he increments it, because it doesn't wait in between increments. To make it fade in and out, you should change the volume in a loop this way:
for i=startingVolume,endingVolume,(endingVolume+startingVolume)/100 do -- this math is fairly simple; this basically means "go from starting volume to ending volume in 100 increments" siren.Volume=i wait() -- If you don't yield, it'll seem instant! end siren.Volume=endingVolume
Or, in reverse, replace siren.Volume=i
with siren.Volume=endingVolume-i
(to make it fade out), and the final line with siren.Volume=startingVolume
. You'll probably want your startingVolume as 0 (or else it would sound weird), but any endingVolume greater than 0 and less than 1 should work.
To make this faster, change /100
to something lesser; that means less steps, meaning less waiting, meaning a faster fade-in/out.