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

Trying to play sounds randomly after random few seconds?

OMG. I cannot seem to find why this isn't working. It's a very simple script wth! Any help would help, obvsly, thanks :)

while true do
	local randomshout = math.random(1,2,3,4,5,6,7) --chooses a random sound to shout
	local randomtime = math.random(1,1.5,2) --random time so shouts seem more random
 	
 	if randomshout == 1 then
		wait(randomtime)
 		script.Parent.Die:Play()
		
 	elseif randomshout == 2 then
 		wait(randomtime)
 		script.Parent.Ha:Play()
 		
 	elseif randomshout == 3 then
 		wait(randomtime)
 		script.Parent.Ah:Play()
 		
 	elseif randomshout == 4 then
 		wait(randomtime)
 		script.Parent.Roa:Play()
 
 	elseif randomshout == 5 then
 		wait(randomtime)
 		script.Parent.Ra:Play()
 		
 	elseif randomshout == 6 then
 		wait(randomtime)
 		script.Parent.Toa:Play()
 
 	elseif randomshout == 7 then
 		wait(randomtime)
 		script.Parent.Kill:Play()
 		
 	end
 	
 	wait(0.1)
end

1 answer

Try something like this:

while true do
    local randomshout = math.random(1, 7)
    local randomtime = math.random(10, 20) / 2  --random time between 1 second and 2 seconds

    if randomshout == 1 then
        wait(randomtime)
        script.Parent.Die:Play()

    elseif randomshout == 2 then
        wait(randomtime)
        script.Parent.Ha:Play()

    elseif randomshout == 3 then
        wait(randomtime)
        script.Parent.Ah:Play()

    elseif randomshout == 4 then
        wait(randomtime)
        script.Parent.Roa:Play()

    elseif randomshout == 5 then
        wait(randomtime)
        script.Parent.Ra:Play()

    elseif randomshout == 6 then
        wait(randomtime)
        script.Parent.Toa:Play()

    elseif randomshout == 7 then
        wait(randomtime)
        script.Parent.Kill:Play()

    end

    wait(0.1)
end

6 comments

0

wow tyvm works I screw up lol

0

If it worked, can you accept the answer to help others find it?

0

I kinda dunno how....

0

Are you in the site chat?

0

@Tic_Games Under where you are posting these replies, there should be a button that says "Accept Answer". Or in the top right corner of the answer.

0

Just saying, this is extremely inefficient and unnecessary, a much more better way is using a table and doing something like table[math.random(1,#table)]:Play(), with this the code will be much shorter, and if you want to add another sound you dont have to copy paste the same thing, just add it to the table! i hope you learnt something from this!

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