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.
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
game.ReplicatedStorage.RemoteEvent:FireServer(plr)
end
end
This needs to be put at the very beginning of your script (that handles the rewards) in ServerScriptService.
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
game.ReplicatedStorage.RemoteEvent:FireServer(plr)
end
end
there is a error on plr its said unknown global “plr”
(This is starting to get off-topic so I urge you to create a new topic for further discussion on events. Your original issue with an infinite countdown loop seems to have been solved by a few previous posts.)
You should read over both of these developers hub articles because they explain events and the Roblox client and server model:
RemoteEvents and RemoteFunctions provide a communication between the client and the server. RemoteEvents provide a one way communication whereas RemoteFunctions provide a two way communication because you need to return a value. Here is a little example of how to you could use RemoteEvents:
(Hierarchy):
(Server script in ServerScriptService):
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.RemoteEvent
RemoteEvent.OnServerEvent:Connect(function(Player) -- Fires when the client fires the server. The first parameter automatically becomes the player.
print(Player.Name.. ": Has fired the server")
RemoteEvent:FireClient(Player) -- Fires the client
end)
(Client Scrip in StarterGui):
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.RemoteEvent
RemoteEvent:FireServer() -- Fires the server
RemoteEvent.OnClientEvent:Connect(function() -- Fires when the server has fired the cliet
print("Fired client")
end)
You don’t need to put the player in FireServer() because the first parameter automatically becomes the player when you use the OnServerEvent on the server.
The reason why you are getting this error is because you aren’t specifying the player. As I explained above you don’t need to put the player in FireServer() because the first parameter automatically becomes the player when you use the OnServerEvent.
The reason why you put the RemoteEvent in ReplicatedStorage is because it is a shared contained between the client and the server. This means both the client and server can access it.