I believe heâs trying to make it so that when the timer hits zero, it rewards the player, than the timer resets back to thirty seconds. You could solve this with a simple
local seconds = 30
while true do
wait(1)
if seconds > 0 then
seconds = seconds - 1
script.Parent.Text = seconds
else
seconds = 30
script.Parent.Text = seconds
-- Award the player here
end
end
Alternatively, cut out the middleman, wait(1) and do
local seconds = 30
while wait(1) do
if seconds > 0 then
seconds = seconds - 1
script.Parent.Text = seconds
elseif seconds <= 0 then
seconds = 30
script.Parent.Text = seconds
--award code
end
end
Either one works of course, but you can skip a step this way.
what if i have another award script from ServerScriptService i want to run it at the same time without delay
my award script:amount1 = 10 --amout to give each time
amount2 = 20
amount3 = 40
amount4 = 80
amount5 = 160
for count = 1, 2 do
wait(timedelay)
for i,v in pairs(game.Players:GetPlayers()) do
if v:FindFirstChild(âleaderstatsâ) and v then
v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount1
end
end
end
wait()
for count = 1, 2 do
wait(timedelay)
for i,v in pairs(game.Players:GetPlayers()) do
if v:FindFirstChild(âleaderstatsâ) and v then
v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount2
end
end
end
for count = 1, 2 do
wait(timedelay)
for i,v in pairs(game.Players:GetPlayers()) do
if v:FindFirstChild(âleaderstatsâ) and v then
v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount4
end
end
end
wait()
while true do
wait(timedelay)
for i,v in pairs(game.Players:GetPlayers()) do
if v:FindFirstChild(âleaderstatsâ) and v then
v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount5
end
end
end
cc: @Secretum_Flamma, I think this is the thread that you are on about:
You should try and avoid wait() and any infinite loop in production code if you can:
Lua is single threaded meaning one task runs at any given time and the task scheduler decides when each task is ran. Each time you add a new script you arenât actually creating a new thread for that script because the script has been coroutined. coroutine or spawn(f) donât actually create new threads like in multi-threaded languages because Lua is single threaded at this present time but instead it is telling the task scheduler to run the code in a manner that looks like it is running at the same time.
You may be thinking why does this affect wait() and I have had no problems with using wait(). Whenever you call wait(), you are yielding the current âthreadâ and letting other âthreadsâ run. The problem then comes when the wait() has finished waiting because it needs to wait for a slot to resume. When your code becomes more intensive wait starts waiting several seconds longer than you specified it to wait.
To avoid using wait() and loops you should always use events when you can because Roblox has events for everything you can think of. For your use case you could use RunService.Heartbeat and check the time difference through tick(). From my knowledge Heartbeat is more reliable than wait and a better option than using a loop. Here is a little bit of code that should serve your use case:
local RunService = game:GetService("RunService")
local Duration = 3
local StartTime = tick()
local EndTime = StartTime + Duration
RunService.Heartbeat:Connect(function()
if tick() >= EndTime then
-- Do what you want to do when the time has reached 0
StartTime = tick() -- Puts the start time to the current time
EndTime = StartTime + Duration
end
end)
Sorry if this code looks messy but you should be able to modify it to fit your use case. You should also note that you shouldnât overuse Heatbeat.
You are correct, waits in general arenât as efficient. Both of our examples suffered this, @waterrunner has listed a better example. Iâve never really done that to tell you the truth, but I can definitely use that as a reference from now on.