So in your code on line 4 your line is sound:Play() but the sound variable is really just a number that the random function returned. In order to get a random sound you must index it from the list of sounds in the array soundlist. Also the random function should be used as math.random(1,#soundlist).Heres the code
local soundlist = script.Parent:GetChildren()
while true do
local soundnum = math.random(#soundlist)
local sound = soundlist[soundnum]
if sound.ClassName == "Sound" then
sound:Play()
end
wait()
end
Note: I also added an if statement which makes sure the instance held in the sound variable is a sound that can be played, so the script doesn't error.
Also, Im not sure if you wanted this but if you play the sound, it will choose another sound and play that immediately afterwards, so you will have multiple sounds playing at once. So if you don't want that to happen you have to wait until the sound is finished playing. The best way to do this is to get the sounds timelength and wait that amount. In the code it would look like this:
local soundlist = script.Parent:GetChildren()
while true do
local soundnum = math.random(#soundlist)
local sound = soundlist[soundnum]
if sound.ClassName == "Sound" then
sound:Play()
wait(sound.TimeLength)
end
wait()
end
1 comment
Make an array of all the sounds, then use math.random to pick a random sound.