Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

How do I make a siren slowly fade in?

Asked by 10 years ago

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)

2 answers

Log in to vote
0
Answered by
Lacryma 548 Moderation Voter
10 years ago

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)
0
That seems to simply turn it on and off still. Again, I'm pretty new at scripting so I'm not sure what's wrong with it. Emdebted 22 — 10y
0
I have edited it, if that is still not what you want then can you give more detail? Lacryma 548 — 10y
0
It's not "what I want" because that script is doing exactly the same thing my current one is. The volume is staying at 1 throughout. There's no fade or anything. The point was when you click it, the sound slowly starts up from 0.1 volume going up to 1 and staying there. When you click again, it goes from 1 back down to 0 and then stops. Emdebted 22 — 10y
0
That should do it. Lacryma 548 — 10y
View all comments (4 more)
0
You never added a wait to the loop... And I would use a coroutine to fade it in and out say it was a false alarm... M39a9am3R 3210 — 10y
0
A coroutine would not make it seem any different, thank you for notifiying me about the wait. However, I did not deserve the downvote. I understand some people don't know how to properly use the voting feature. Lacryma 548 — 10y
0
You mean like whoever downvoted my question? I can't even vote, so if it was in response to that that was just dumb. Bu thanks for the negative rep over asking a question, isn't that what this site is for? Emdebted 22 — 10y
0
I downvoted your answer back then because it was wrong, plain and simple. You downvote answers that are wrong and upvote ones that are right. That's how it is meant to work. I'm removing the downvote now (as you corrected it). TheLuaWeaver 301 — 10y
Ad
Log in to vote
4
Answered by 10 years ago

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.

0
I'm sorry, but I'm really new to this. How would it all fit together with the current script? Emdebted 22 — 10y

Answer this question