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

How do i fix this script? (Audio)

while true do
local song = script:findFirstChild(math.random(1,4))
song:play()
wait(2)
end

I have 4 sounds from (1,2,3,4) renamed. There diffrent songs. So, I added wait(2) and the above things. I tested this out and I didnt get the right results. I want to loop 4 songs together. What am i missing?

script : 1 2 3 4

1 answer

✓ Accepted answer

To find a random a random child, you would use the GetChildren() function.

Here is an example on you could use this:

while true do
local song = script:GetChildren()[math.random(1,4)]
if song.ClassName=="Sound" then-- Just to make sure it's an audio/sound instance
song:play()
end
wait(2)
end

GetChildren() returns a table of all of the children in a specific instance.

I hope this helped you with your problem!

Read More On GetChildren() here

YOU CAN ALSO USE AN ALTERNATIVE WAY! You can use ToString() since the Name Property is a string value. Tostring() converts numbers to string values. To use this alternative way, you must have the sounds numbered as numbers.

while true do
local song = script:findFirstChild(tostring(math.random(1,4)))
song:play()
wait(2)
end

1 comment

0

thanks.

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