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

How can i make a sound play after some time?

Asked by
notfenv 171
5 years ago

Basically, i'm gonna make something thats called King Headless, He makes an evil laugh every 20 to 15 seconds, What do i need to do to make that possible?

while true do

wait(0.5)

script.Parent.EvilLaugh:Play()


end

Please Write your examples in answers, i do not understand through comments.

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago
while true do
wait(math.random(15,20)

script.Parent.EvilLaugh:Play()
script.Parent.EvilLaugh.Ended:Wait()

end

Hope This Helps!

0
Make sure to put a 'math.randomseed(tick())' in the first line of code, It makes the randomness even more random Vinceberget 1420 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

To make your cooldown from 15-20 seconds you'll need to change the wait() time.

local sound = script.Parent:WaitForChild("EvilLaugh")
while true do
    wait(17.5) -- average of 15 and 20
    sound:Play()
    wait(sound.TimeLength)
    sound:Stop()
end

The 'wait(sound.TimeLength)' bit will make it so that the sound will stop after it's done playing.

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

This is quite simple, you’re using the correct loop, but I’d use repeat as it feels a little bit cleaner.

local EvilLaugh = script.Parent.EvilLaugh --//Get used to reference variables, they help juristically
repeat wait(25) --// time between each “laugh”; before the next run.
   EvilLaugh:Play()
      wait(EvilLaugh.TimeLength)
   EvilLaugh:Stop() --// Extra precaution if the sound doesn’t stop
until --// you should probably provide a conditional statement to break the loop as if something breaks have by the loop processing nothing forever.

Hope this helps!

Answer this question