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

I need help with making a script that plays a random sound from a list of 3 sounds. Help?

Asked by 5 years ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.
local One = script.Parent.break1:Play()
local Two = script.Parent.break2:Play()
local Three = script.Parent.break3:Play()

while true do
wait(1.3)
math.random(One,Two,Three)
end

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Tables

A table is a datatype that can hold other values. They can store strings, booleans, numbers, objects, functions and other tables. What makes up a table are a bunch of keys. Each key has different values which you can assign to them. By default, every key is a number in consecutive order, like so:

local tab = {true, false, "Text", workspace.Sound}

You can get a single value by getting the table and use one of the two bracket or dot operators to get the specific value of a specific key.

print(tab[1]) --Prints the first item in the table

true

tab, in that example, is the table. The 1 is the key. We print out the value of 1. The keys can be set to anything you want. When you do this, it's called a dictionary.

local dictionary = {
["ROBLOX"] = "An online video game",
["America"] = "A continent in North America",
["Violin"] = "A 4-stringed wooden instrument played with a bow"
}

print(dictionary["ROBLOX"])

The way we get the value of the key "ROBLOX" is the same way we get the value of the key 1. I mentioned earlier that you can use the dot operator as well, but I've only been using square brackets. You can get the value out of a table when the key doesn't start with a number and is not a string.

local dictionary2 = {
["String"] = "Text",
Booleans = "Either true or false"
}

print(dictionary2["String"])
print(dictionary2.Booleans)

Text Either true or false

They're slightly different, as the dot operator cannot get anything that starts with a number or has a space, just like how workspace.BlackPart works but workspace.Black Part doesn't. workspace["Black Part"] has to be used instead.


math.random & math.randomseed

math.random() is a pseudo-random number generator. It actually doesn't create fully random numbers each time and has a default seed it always follows. If you want, you can use math.randomseed to change the seed that ROBLOX uses to get random numbers.

math.randomseed(tick())
--tick() is the amount of seconds from the UNIX epoch, or January 1st, 1970, on your local machine

math.random() has 2 arguments. Only 1 argument is mandatory, but more than 2 will not work. If you only put 1 number in, lets say x, it'll get any "random" number from 1 to x. If you put 2 numbers, x and y, it'll get any "random" number from x to y. More information can be read here.

print(math.random(5))
print(math.random(5,10))

Output may vary: any number 1-5 then any number 5-10.


Final Product

We will use a table to store all of your sounds inside. We will then use the RNG to get a random key and value.

math.randomseed(tick())
local items = {script.Parent.break1, script.Parent.break2, script.Parent.break3}

while true do
    wait(1.3)
    items[math.random(#items)]:Play()
end

If you're wondering what the # operator does, it tells you the length of the table in this example. Since there are 3 items in the table, it's the same exact thing as saying 3. Alternatively, all your scripts have the same name and the only differing factor is the number at the end. You can use the :FindFirstChild() to get the sound from script.Parent that has the name break and any number at the end.

math.randomseed(tick())

while true do
    wait(1.3)
    script.Parent["break"..math.random(3)]
end

If you are wondering what the two dots are for (..), they mean to concatenate, or put together. We are combining the name break with a random number 1-3. This will give us 3 results: break1, break2 or break3.



Hope it helps!

Ad
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

Make a table with the sounds in it, and then use a variable for the random sound. Then use the play function with the variable.

local randomsounds = {
    script.Parent.break1,
    script.Parent.break2,
    script.Parent.break3    
    }

local sound = randomsounds[math.random(1, #randomsounds)]
while true do
wait(1.3)
sound:Play()
end
1
That won't give you a random sound every time there is a loop. It will just loop the same sound over and over since you are defining the variable `sound` as a random sound but never changing it again. Swap lines 7 and 8 to get a script that will give you a random sound each loop. EzraNehemiah_TF2 3552 — 5y
0
True! ShinyGriffin 129 — 5y

Answer this question