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

How do i fix this script? (Audio)

Asked by 10 years ago
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

Log in to vote
0
Answered by
Dominical 215 Moderation Voter
10 years ago

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
0
thanks. legoson7 70 — 10y
Ad

Answer this question