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

How do I make a random sound player using tables?

Asked by
Stea_my 48
5 years ago

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
0
Don't forget to accept my answer if it solved your problem. User#24403 69 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

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
2
epic starmaq 1290 — 5y
Ad

Answer this question