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.
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)
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?
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)
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?
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?
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
Players.PlayerAdded:Connect(function(player)
task.spawn(function()
while true do
-- wait a random amount of time
local durationToWait = math.random(1,5)
local timeWaited = task.wait(durationToWait)
-- 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
Players.PlayerAdded:Connect(function(player)
-- get ranodom time
local durationToWait = math.random(1,5)
DurationTables[player] = {0, durationToWait}
end)
task.spawn(function()
-- repeat this all the time
while true do
-- keep track of how much time has passed
local deltaTime = task.wait()
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)
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.