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

How Do I Make A Script That Play A Random Song When Click?

Asked by
ImfaoXD 158
8 years ago

This is all I could do and I don't know how to make the click detector play a random song when click. Can you guys help me out?

Brick = script.Parent
Sound = Brick.Sound
Debounce = false

function onClicked()
    if not Debounce then -- If the debounce is false
        Debounce = true -- Change it to true
        Sound:Play()
        wait(Sound.TimeLength) -- Wait til it ends
        Debounce = false -- Change it to false
    end -- End the if function
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Many thanks, my friends!

1 answer

Log in to vote
0
Answered by
Necrorave 560 Moderation Voter
8 years ago

I think the best method for doing this is storing your sounds in one folder or model.

Then you would create an array variable by using the :GetChildren() method.

From there you could create a random number using math.random() to choose one of the sounds inside the array.

Example:

sounds = game.Workspace.Sounds:GetChildren() -- This would be where I stored all the sounds

local rand = math.random(#sounds)-- #sounds will be the amount of sounds in the array

sounds[rand]:Play() -- This would take the random sound and play it.

When working with arrays, keep in mind that each child is assigned to an index or number within the array.

Whatever number you put between the [] will play the sound assigned to that. With Lua, the indexing starts at 1. So if you had 3 sounds sounds[2]:Play() would play the second sound in the list.

More info on arrays http://wiki.roblox.com/index.php/Table

Let me know if you have any questions!

Ad

Answer this question