Detecting if 10 seconds have passed

Hi, i’m using this new way (don’t know what it’s called) to countdown instead of a for loop because of how it runs better, but how do I detect if 10 seconds have passed? Here is my code:

if i == i - 10 then
		print("It's time")
	end
	
	while i >= 0 do
		i -= 1
	end
2 Likes

Uh, truth is, they’re both equally performant. Regardless, I don’t quite understand what you’re trying to do in this code sample, but I can only assume you were trying to do something like this?

i = 10

while i >= 0 do
	i -= 1
    wait(1)
end
print("It's time")

It’s a countdown, the for loop has shown that it takes longer to countdown. Length is a parameter I set, so it won’t be 10, more like 600.

Explain what exactly this is supposed to be used for please, because I don’t understand what exactly you’re doing. Your original code has no wait, so I don’t know if this is meant to be something with specific timing, or…

It doesn’t matter how you do it, the performance won’t change.

If you want to countdown once:

for i = 10,1,-1 do
    wait(1)
end
print("10 seconds has passed")

If you want to countdown infinitely:

local i = tick()

while wait() do
    if tick() - i >= 10 then
        print("10 seconds has passed")
        i = tick()
    end
end
1 Like

This code is inside a function with parameters. Here’s a portion of the code:

function module.StartFFGRound(length, chosenMurderer, map) -- in seconds
	local i = length
	
	if i == i - 10 then
		print("It's time")
	end
	
	while i >= 0 do

		wait(1)

		i -= 1
	end	

end

The best alternative here is probably to use heartbeat from RunService. (If you don’t need the code to yield)

The heartbeat event has a single argument that describes how much time in seconds has passed since last frame. All you have to do is to keep track of the current time and subtract dt from it each frame. Once the countdown is 0 or less, the time has passed.

local countdown = 10
local heartbeatConnection

heartbeatConnection = RunService.Heartbeat:Connect(function(dt)
    countdown -= dt
    if countdown <= 0 then
       print("It's time")
       heartbeatConnection:Disconnect() --Stop the timer
    end
end)
3 Likes

Why are you checking if i is equal to itself minus 10? That will never evaluate to true.

Regardless,

function module.StartFFGRound(length, chosenMurderer, map) -- in seconds
	local i = length
	
	while i >= 0 do
		wait(1)
		i -= 1
	end	
	print("It's time")
end

This should work.

1 Like
function module.StartFFGRound(length, chosenMurderer, map) -- in seconds
    for i = length,1,-1 do
        wait(1)
    end
    print("It's time")
end

I want both of the while and for loop to run at the same time, if I do this the countdown won’t start until 10 seconds have passed. The reason why I am using a while loop is because the for loop is slower with the code inside of it.

How come you want the countdown to start while there’s 10 seconds passing? What’s the point of this?

If you noticed any difference in speed in a simple wait loop between a for loop and a while loop, there was an issue with the code you used.

I probably should better explain this. There is 600 seconds (the length) for a game, after 10 seconds have passed, I want the murderer to teleport to the map. That’s why I need help trying to figure out how 10 seconds have passed.

@JaksonNX @OIogist

function module.StartFFGRound(length, chosenMurderer, map) -- in seconds
	local i = length
	
	while i >= 0 do
		wait(1)
		i -= 1
	end	
	-- Teleport everyone else.

	delay(10, function()
		-- Teleport murderer in.
	end)
	
	delay(length, function()
		-- End game?
	end)
end

Delay on the last one is optional, only if you don’t want code to yield

Then just teleport all the players (excluding the murderer), then teleport the murderer after 10 seconds.

local murderer = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers)]
local countdown = tick()

for _, plr in pairs(game.Players:GetPlayers()) do
    if plr ~= murderer then
        plr.Character.HumanoidRootPart.CFrame = --spawn
    end
end

local connection = game:GetService("RunService").Heartbeat:Connect(function()
    if tick() - countdown >= 10 then
        murderer.Character.HumanoidRootPart.CFrame = --spawn
        print("Time has passed")
        connection:Disconnect()
    end
end)

I’m lost, I’ll just provide the whole module at this point since I am not explaining it well:

function module.StartFFGRound(length, chosenMurderer, map) -- in seconds
	local outcome

	game.ReplicatedStorage.Events.IdentifyPlayer:FireAllClients()
	game.ReplicatedStorage.Values.GameInProgress.Value = true
	local i = length
	
	if i == i - 10 then
		print("It's time")
		module.TeleportMurdererToMapFFG(chosenMurderer, map.PlayerSpawns:GetChildren())
	end
	
	while i >= 0 do
		local innocents = {}
		local murderer = {}
		local murdererHere = false
		

		for i, player in pairs(game.Players:GetPlayers()) do
			if player:FindFirstChild("Innocent") then
				table.insert(innocents, player)
			elseif player:FindFirstChild("Murderer") then
				wait(.3)
				murdererHere = true
				table.insert(murderer, player)
			end
		end

		status.Value = toMS(i)
		if not murdererHere then
			outcome = "murderer-left"
			break
		end
		if #innocents == 0 then
			outcome = "murderer-victory"
			break
		end
		if i == 0 then
			outcome = "time-up"
			break
		end

		wait(1)

		i -= 1
	end	


	if outcome == "murderer-victory" then
		local winner

		for i, v in pairs(game.Players:GetPlayers()) do
			if v:FindFirstChild("Murderer") then
				winner = v.Name
			end
		end
		status.Value = winner.." is the winner!"
	end
	if outcome == "time-up" then
		status.Value = "Times up! No one wins!"
	end
	if outcome == "no-one" then
		status.Value = "No one was the last standing!"
	end

	wait(5)
end

A Heartbeat connection would work very well in this situation.

local roundTime = 600
local teleportedPlayer = false

RunService.Heartbeat:Connect(function(dt)
    roundTime -= dt --Reduce the delta time from the current round's time
    if roundTime <= 590 and teleportedPlayer then
        --Teleport the player here
        teleportedPlayer = true
    end
end)

If you want multiple “phases” within your round, my idea is to store a function to call when a specific timeframe is reached, and then mark it as the phase the round is currently in. I can go more in depth if you want.

1 Like

Still didn’t work. This situation is a lot harder than I expected it haha

If there is no way to break the wait then you could use “delay”

delay(10, function()
–Delay accepts two arguments. First is wait time and second is a function that fires after the wait
end)

If you have to edit smth like UI, then I believe for loop is the best way.