Hello, fellow Roblox developers!
Is there any way to make the wait() value random? I have searched on the Devfourm, but I haven’t found anything. I know it has something to do with math.random, but I don’t know exactly what.
Sorry for bothering you, I’m just a terrible scripter!!
you can use wait(math.random(1, 4))
which would wait 1 to 4 seconds
Sorry, the numbers 1 and 4, or all the numbers in between?
To go more into detail of what TOP_Crundee123 has provided, math.random
is a function which will randomly pick a number from your min by your max. so for what he provided, math.random would pick the numbers of 1 being the min through 4 being the max. math.random can also be very useful for picking a random player. Here is an article for more information on math
: math | Documentation - Roblox Creator Hub
Edit: I would definitely recommend using this for picking random players, like SharkBite or randomly picking a prize someone might win. Your way also will work as well.
wait(math.random(1,6)) -- remember 1 is the min, 6 is the max, but it can also choose numbers in between
Thank you!! That helped a lot.
If you’d like decimal waiting times (since math.random
only returns integers) then you can use the Random datatype, it’s similar yet to create a Random datatype you use: Random.new(seed: number)
and from that object returned you can use it’s NextNumber method (that takes directly the same arguments).
For example:
local Rand = Random.new()
wait(Rand:NextNumber(1, 6))
correct me if i’m wrong but i believe that math.random
does return decimals, but only when you leave out all arguments… math.random()
print(math.random()) --->>> 0.29865786701296
But this isn’t very good since it only will return number between 0-1, and to add in a range you would have to do something like this:
math.random() + math.random(1,6-1)
Which only adds to the “complexity” so overall I think using what you said is better.
I can provide you a easier solution for you:
local Max_Number = 10
local Min_Number = 1
local Random = math.random(Min_Number, Max_Number)
wait(Random)
What I do when I generate random numbers is a little unique but limited.
For example if I want numbers between 5 to 10 and I want them in decimals as well:
math.random() * 5 + 5
would return various numbers.
Just a tip
wait(math.random(1,10)) – 1 is the minimum time that could be waited, and 10 is the max
Yeah, that’s right. protip: You can use math random to select random objects! If you are interested, message me
@ReturnedTrue gave the correct solution to this.
Using the new Random
type is preferred (although I think math.random
now uses it internally). Using NextNumber
will give you a number between the range.
local r = Random.new()
r:NextNumber(1, 10) SAME AS 1 + math.random() * 9
r:NextInteger(1, 10) SAME AS math.random(1, 10)
I was looking for the same thing thanks that helped me a lot!!