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

How do I successfully play sounds in a part?

Asked by 3 years ago
Edited 3 years ago

Hey, so I am trying to make this code emit a random sound from a part. I also want it to play another sound after the other is done playing. These sounds are children of the part. I wrote this script but it's not playing the sound, I guess it sees 'randomItem' as a number.

local items = script.Parent:GetChildren()

for _, v in ipairs(script.Parent:GetChildren()) do
    if v.ClassName == "Sound" then
                v:GetChildren(items)
        local randomItem = math.random(#items)
    print(#items)
        randomItem.Playing = true
    end

end


Are there any other methods of doing this better? Please let me know if there are, thanks!

1 answer

Log in to vote
1
Answered by 3 years ago

Yeah I think I can help with that

This is wrong If your trying to get a random Item

local randomItem = math.random(#items)

But this is how you do it

local randomItem = items[math.random(1, #items)]

math.random needs 2 values, minimum and maximum numbers. so you can get one item in the Array called "items" by saying "item[number here]" the random number will go there

Please accept if this helped

0
Thank you! There is still one problem though, it plays all of the sounds at once, do you know how that can be fixed? If not, that's alright. Ashton011 30 — 3y
Ad

Answer this question