Need help with this loop

Im using ZonePlus and trying to make it so player gets damaged when they are in a zone.
But my loop doesnt end if i leave the zone.

icebath.playerEntered:Connect(function(player)	
	print("player has entered the zone")
	local isWithinZoneBool = icebath:findPlayer(player)
	local health = game.Workspace:WaitForChild(player.Name).Humanoid
	while true do 
		wait()
		if isWithinZoneBool then
			health.Health -= 15
			wait(1)
		elseif not isWithinZoneBool then		
		end
	end
end)

Reason is very likely due to you not having a break to end the loop when the player exits the zone.

added a Break after elseif ont isWithinZoneBool Then and didnt stopped

You never refresh isWithinZoneBool. Also, the loop condition should be isWithinZoneBool.

icebath.playerEntered:Connect(function(player)	
	print("player has entered the zone")
	local health = game.Workspace:WaitForChild(player.Name).Humanoid
	while icebath:findPlayer(player) do
		health.Health -= 15
		task.wait(1)
	end
end)

I edited the code to be better

2 Likes

Ah yea the issue is that you don’t keep checking if the user is in the zone or not. You check once outside of the loop and then you just check the exact same thing over and over. Put the isWithinZoneBool inside of the while true loop.

Basically what @Kaid3n22 said but I don’t think it is explained very well how they put it because of them just saying that the variable is never refreshed which is not really the correct terminology in my option.

1 Like

ok i guess i really am idiot thank you.

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