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 successfully play sounds in a part?

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

✓ Accepted answer

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

1 comment

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.

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