Multiple Waits In While Loop

  1. What do you want to achieve?
    I’m trying to add cash every second but I also want to fire a RemotEevent every 30 seconds.

  2. What is the issue?
    The add cash is working but I want to add another while loop or wait to fire a RemoteEvent every 30 seconds.

  3. What solutions have you tried so far?
    I tried adding another while loop but I know that any line of code after a while loop won’t run.

local Players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")

local dataStore = dataStoreService:GetDataStore("Test")

Players.PlayerAdded:Connect(function(Plr)
	local Statistics = Instance.new("Folder")
	Statistics.Name = "leaderstats"
	Statistics.Parent = Plr
	
	local Cash = Instance.new("NumberValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = Statistics
	
	local Multiplier = Instance.new("NumberValue")
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 1
	Multiplier.Parent = Statistics
	
	local Life = Instance.new("NumberValue")
	Life.Name = "Life"
	Life.Value = 0
	Life.Parent = Statistics
	
	local Key = "ID-"..Plr.UserId
	local getSaved, message = pcall(function()
		Data = dataStore:GetAsync(Key)
	end)
	
	if getSaved then
		Cash.Value = Data[1]
		Multiplier.Value = Data[2]
		Life.Value = Data[3]
	end
	
	if not getSaved then
		warn(message)
	end
	
	while wait(1) do
		Cash.Value = Cash.Value + math.random(1, 10) * Multiplier.Value
	end
end)

Players.PlayerRemoving:Connect(function(Plr)
	local Key = "ID-"..Plr.UserId
	local toSave, message = pcall(function()
		dataStore:SetAsync(Key, {Plr.leaderstats["Cash"].Value, Plr.leaderstats["Multiplier"].Value, Plr.leaderstats["Life"].Value})
	end)
		
	if not toSave then
		warn(message)
	end
end)
``

local count = 0

while wait(1) do
Cash.Value = Cash.Value + math.random(1, 10) * Multiplier.Value

count = count+1

If count == 30 then
count = 0
—CODE HERE

end
end

1 Like

I typed this in my mobile device , I apologize for any bad syntax.

1 Like

Oh that makes sense, thank you.

You can use coroutines to achieve what you want.

coroutine.wrap(function()
    while true do
        Cash.Value = Cash.Value + math.random(1, 10) * Multiplier.Value
        wait(1)
    end
end)()
coroutine.wrap(function()
    while true do
        -- fire remote event
        wait(30)
    end
end)()

But remember that the loop has to be stopped after a player is removed so I advice to add a check to the loop.

if Plr.Parent == nil then 
    break 
end

This is the easiest solution, but not the best.
I would make only one loop that iterates through all players giving them money every one second and another loop that fires the Remote Event (FireAllClients) for every player every 30 seconds. There’s no need to do that for each player separately.

1 Like

If you wanted to be more prescise with adding these at certain times you could use a Heartbeat Connection and add the DeltaTime to an Accumulated value, then check if the Accumulated value is higher than the amount of time to wait for.

Connections are asynchronous and therefore don’t require to be coroutine wrapped.

Here’s an example:

local RunService = game:GetService("RunService");
local INTERVAL = 5;
local Accumulated = 0;

RunService.Heartbeat:Connect(runction(DeltaTime)
    Accumulated = Accumulated + DeltaTime;

    if (Accumulated >= INTERVAL) then
        print("This'll only print every 5 seconds!");
        Accumulated = 0;
    end
end)
1 Like

You will need to reset the Accumulated to 0 or it won’t fire every 5 seconds, but every time heartbeat fires after 5 seconds have passed.

1 Like