How (and When) to Run an Event inside of a Round Loop

Within my round sequence, I am trying to run an event at a particular time. However, my programming knowledge is a bit lacklustre, and my only thinking is to create an if statement at a particular time. When I do this, though, nothing happens (e.g., if i == 150 then [call event]). Therefore, I am wondering what I can do to achieve this and where I should call it.

Script for Critique:

-- There is coding that runs before this loop
local RoundTime = 300 -- How long the map will remain in the workspace
	for i = RoundTime, 0, -1 do
		Status.Value = "The Agents must Terminate the MALWARE within the Time Limit"
		local Mactive = false
		local Agents = {}
		for i, Player in pairs(game.Players:GetPlayers()) do
			if Player:FindFirstChild("Agent") then
				table.insert(Agents, Player)
			elseif Player:FindFirstChild("MALWARE") then
				Mactive = true
			end
		end
		if not Mactive then
			Status.Value = "The Agents have terminated the MALWARE!"
			Round.AgentVictory()
			task.wait(5)
			break
		end
		if #Agents == 0 then
			Status.Value = "The MALWARE has Eliminated the Agents!"
			Round.MALWAREVictory()
			task.wait(5)
			break
		end
		if i == 0 then
			Status.Value = "The Agents have Failed to Terminate the MALWARE within the Time Limit!"
			Round.Stalemate()
			task.wait(5)
			break
		end

		Timer.Value = toMS(i)
		task.wait(1)
	end
-- End of code of a larger game sequence

Does this section of code work? As I can’t see any problems with your initial thoughts of adding an if statement similar to this. As you are already checking the value of i here, I would use an elseif to keep it organised.

If this section doesn’t run then you must be breaking the loop before you reach here, so try printing the values of Mactive and agents to check they aren’t triggering unexpectedly.
You could also try printing i to make sure it is decrementing.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.