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
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")
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…
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)
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.
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.
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.