Safezone Bug where time doesn't stop when entering zone again

Hello there, I’ve been trying to solve this issue, where people get their time added when they’re out of a safe zone. The issue is that when the player enters into the safe zone again, the time continues and the loop running wont stop. Break doesn’t work either.

What I am trying to achieve is that you actually can stop the time when entering safe zone again.

Server Script:


game.ReplicatedStorage.Remotes.AddTime.OnServerEvent:Connect(function(plr, bool )
	local leaderstats = plr:WaitForChild("leaderstats")
	local ctime = leaderstats:FindFirstChild("Time")
	
	while wait(1) do
		if bool == true then
			ctime.Value = ctime.Value + 1
		elseif bool == false then
			break
		end
	end
		
end)

Local Script

safezone.localPlayerEntered:Connect(function()
	local inZone = safezone:findLocalPlayer()
	if inZone then
		game.ReplicatedStorage.Remotes.Safe:FireServer()
		game.ReplicatedStorage.Remotes.AddTime:FireServer(false)
		print("In Zone")
	end
	
end)

safezone.localPlayerExited:Connect(function()
	local inZone = safezone:findLocalPlayer()
	if inZone then
	else
		game.ReplicatedStorage.Remotes.SafeExit:FireServer()
		game.ReplicatedStorage.Remotes.AddTime:FireServer(true)
	end

end)

Your break doesn’t work because this would just make a new while loop, so it wont disconnect the others

If you want it to work, have a dictionary set up for the players that triggered the event and set the value to the boolean, and have hte loop work off of that

Try this

local timePlayers = {}

game.ReplicatedStorage.Remotes.AddTime.OnServerEvent:Connect(function(plr, bool )
	local leaderstats = plr:WaitForChild("leaderstats")
	local ctime = leaderstats:FindFirstChild("Time")
	
	timePlayers[plr] = bool
	
	while timePlayers[plr] do
		wait(1)
		ctime.Value += 1
	end
	
	timePlayers[plr] = nil
end)
1 Like

Thank you so much, Appreciate it!

1 Like

You shouldn’t be handling zone entry on the client because an exploiter could just fire the remotes to bypass your safezones. With your current setup the exploiter could just do this

for i = 1, 100 do
    game.ReplicatedStorage.Remotes.AddTime:FireServer(true)
end

and give themselves 100 time per second

1 Like