Help fixing for i loop

im trying to make it so that when there’s only one person, a song plays and the timer is automatically set to 90 seconds. however it doesn’t change to 90 and instead keeps counting down from wherever it is at

local lms = false
		for i = 180, 0, -1 do
			message:FireAllClients(i)
			task.wait(1)
			if (#getplayersingame() <= 0) then
				cleanup()
				message:FireAllClients("The Killer Wins!")
				song.creationofhatred:Stop()
				events:WaitForChild("EndCinematicBars"):FireAllClients()
				task.wait(3)
				break
			elseif i == 0 then
				cleanup()
				message:FireAllClients("The Survivors Win!")
				events:WaitForChild("EndCinematicBars"):FireAllClients()
				task.wait(3)
				break
			elseif (#getplayersingame() == 1) and lms == false then
				lms = true
				i = 90
				song.creationofhatred:Play()
				events:WaitForChild("LMS"):FireAllClients()
				if workspace:FindFirstChild("KillerThemes") then
					workspace.KillerThemes:Destroy()
				end
				task.wait(8)
				events:WaitForChild("CinematicBars"):FireAllClients()
			end
		end
        end
end

i THINK THE ISSUE IS CODE ITSELF BRO :skull:
No offense but its SCARY.

While you can assign values to i, it may overwrite it. I’ve come up with my own solution for a project a bit ago:

local i = 180

while i > 0 do
i = i - 1
-- your logic here
i = 90
end

You cannot change the definition of i while inside the loop.

You should research how bigger games actually manage their rounds.

For example, I created my own round manager system. It’s fairly rudimentary and still in-development, so you should NOT use it, nor do I give you permission to use it.

It looks like this:

local Signal = require(script.Parent.GoodSignal)

local RoundManager = {}
RoundManager.__index = RoundManager

function RoundManager.new()
	local self = {
		["RoundInProgress"] = false,
		["RoundStartEvent"] = Signal.new(),
		["RoundEndEvent"] = Signal.new(),
		["CountdownEndEvent"] = Signal.new()
	}
	
	return setmetatable(self, RoundManager)
end

function RoundManager:StartRound()
	if self.RoundInProgress then return warn("Round already in progress, end round to start a new one") end
	self["RoundInProgress"] = true
	self["RoundStartEvent"]:Fire()
end

function RoundManager:EndRound()
	self["RoundEndEvent"]:Fire()
	self["RoundInProgress"] = false
end

function RoundManager:OnRoundStart(cb)
	self["RoundStartEvent"]:Connect(cb)
end

function RoundManager:OnRoundEnd(cb)
	self["RoundEndEvent"]:Connect(cb)
end

function RoundManager:StartCountdown(_time: number, cb)
	for i=_time, 0, -1 do
		task.wait(1)
		print(i)
	end
	
	self["CountdownEndEvent"]:Fire()
	cb()
end



return RoundManager
1 Like