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

How do I make a random sound player using tables?

I want to make a random sound player for a game but I can't figure it out, heres my code:

local sounds = {script.Parent.Sound, script.Parent.Sound2, script.Parent.Sound3}

while true do 
	wait(math.random(0.5, 5))
sounds(math.random):Play()
end

1 comment

0

Don't forget to accept my answer if it solved your problem.

1 answer

This code doesn't work because of line 5

sounds(math.random):Play()

This would cause attempt to call a table value error. What you want to do is index the table for a random sound.

local sounds = {script.Parent.Sound, script.Parent.Sound2, script.Parent.Sound3}

while true do 
    wait(math.random(1, 5))
    local sound = sounds[math.random(#sounds)] 
    sound:Play()
end

#Walkthrough

  • Indexing the table for a random sound
  • Store sound in a sound variable
  • Play that sound

1 comment

2

epic

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