# Randomizing orders

Hello,

So currently i’m making a script that generates random orders on a random time. For now I only need to know how I can make it happen on a random picked time.

For example when the player joins the script randomizes a time that is 4 minutes after the player had joined. So when the player is in the game for 4 minutes straight the order will be visible for that player. And 2 minutes after that another order will be visible for him/her.

But I have no idea where I need to start.

``````game.Players.PlayerAdded:Connect(function(player)
local randomWait = 240 + math.random(10, 30)
wait(randomWait)
-- order will be visible

while true do
local randomWait = 120 + math.random(10, 30)
wait(randomWait)
-- order will be visible
end
end)``````
1 Like

Ok, I see what you did there but the the problem is that there could be hundreds of orders if the player is in the game for a couple of hours. And I don’t think that i’m going to do while true do the entire time. So is there a simpeler way to do that?

How many times do you want the players to have orders (max orders)?

Well to make sure that the player’s have enough orders and I don’t think that they will play the game for 5 hours straight. Lets say 100 orders.

you can use `for loops` to have a set amount of loops (also i fixed the script)

``````game.Players.PlayerAdded:Connect(function(player)
local startingWait = 240 + math.random(10, 30)
wait(startingWait)

for i = 1, 100, 1 do
-- order will be visible
local randomWait = 120 + math.random(10, 30)
wait(randomWait)
end
end)
``````
1 Like

Now in the for loop you have 120 + math.random 10, 30 so it always takes 2 minutes plus the random number that comes out the math.random right? And if the player joins he will always have an order?

1 Like

This question is a whole lot different from this topic but still i’m gonna ask.

I have some TextButtons in a Frame and those Buttons contains the order. With its specific script, now how do I apply that script to that order? And how do I make it that if the top left TextButton already is visible that the Button on the right side of it needs to show?

Sorry that I keep bothering you!

This is probably a cool way to do it? Most practical I have no idea . Anyways i came up with two solutions.

Solution 1 )
Give each player a dedeicated thread and yeild until code is to be runned.

``````
local Players = game:GetService("Players")

--  when each player joins
while true do
-- wait a random amount of time
local durationToWait = math.random(1,5)
-- Run Code
end
end)
end)
``````

Solution 2 )
Keep track of how much time has passed and how long to wait for each one

``````local Players = game:GetService("Players")
local DurationTables = {}
-- save a table for each player
-- get ranodom time
local durationToWait = math.random(1,5)
DurationTables[player] = {0, durationToWait}
end)

-- repeat this all the time
while true do
-- keep track of how much time has passed
for i,DurationTable in ipairs(DurationTables) do
DurationTable[1] += deltaTime
-- compare the current passed [1] and the amount of time to wait [2]
if DurationTable[1] > DurationTable[2] then
-- Run code

-- set new time
local durationToWait = math.random(1,5)
DurationTable = {0, durationToWait}
end
end
end
end)
``````

Treat above code like psudeo code ( E.G, A general idea of how to do it)

Does this code works as good as @VeriBaesix script? Cause his script is a bit simpeler.

Hmm, maybe. I thought you wanted to ‘orders’ to repeat.

I used print() statements to check what’s going on but the output isn’t giving int values but numbervalues all I can see is 0.01787943058493 and so on.

Yes I did, but his script can repeat too? Or am I misunderstanding something?

Oh, then lol its the same. Only difference is that i save the timeWaited, and put in a coroutine.

1 Like

Can you guys answer this too? That would even help me out more.