Entering a safezone makes your time stay the same

I have a script which makes your time go up by 1 every second. My game is a sword fighting game and I want a safezone which makes you invincible, but people could use this to farm time so I want to make your time stay.

Im using a touched event to detect when you go in a safe zone.

local players = game:GetService("Players")
player = players.PlayerAdded:Wait()
print(player.Name)


local coro = coroutine.create(function()
	while task.wait(1) do
		player.leaderstats.Time.Value -= 1
	end
end)

function whentouchedOn(hit)
	coroutine.yield(coro)
	if not hit.Parent:FindFirstChild("Humanoid") then return end
	if hit.Parent:FindFirstChild("ForceField") then return end
	local forcefield = Instance.new("ForceField", hit.Parent)
	forcefield.Visible = false
	
end
script.Parent.Touched:Connect(whentouchedOn)

function whentouchedOff(hit)
	coroutine.resume(coro)
	if not hit.Parent:FindFirstChild("Humanoid") then return end
	if hit.Parent:FindFirstChild("ForceField") then
		hit.Parent.ForceField:Destroy()
	end
end
script.Parent.TouchEnded:Connect(whentouchedOff)

The problem is your time continues to go up and no errors

You can just not use coroutines and check if the player is in the safe zone when adding a second.

like using a bindable event? or something

I personally recommend ZonePlus2.

What is that?

dont mind this here

It seems the model has been taken down. Do you have like a copy of it?

Zone.rbxm (32.9 KB)
for some reason autocomplete dosnt work

Use a raycast to determine if a player is in a safe zone and do not increase their time by firing an event.

You could use Part:GetPartBoundsInBox() to see which parts are inside a specified part. (or Part:GetPartBoundsInBox() for more precise results)

This function returns a table, so you could look for a HumanoidRootPart, check its parent’s name to get the player’s name, and put the player in a “paused” list. The function you use to give players time could check whether the player is paused or not.

Example:

local Players = game:GetService("Players")

local Safezone = workspace.mySafeZone

while task.wait(1) do
	local safePlayers = {}

	local parts = Safezone:GetPartBoundsInBox()

	for _, part in pairs(parts) do
		if part.Name ~= "HumanoidRootPart" then return end
		if not part.Parent:IsA("Model") then return end
		if not Players:FindFirstChild(part.Parent.Name) then return end

		table.insert(part.Parent.Name)
	end

	for _, plr in pairs(Players:GetChildren()) do
		if table.find(safePlayers, plr.Name) then return end

		-- give player +1 second time
	end
end