Need help with periodicaly damaging area (utilizing ZonePlus)

(if you don’t know what ZonePlus is, check it out here)

I put a script inside a part to periodically damage the player whenever they enter, however, if they enter and exit multiple times, the loops stack on one another, causing rapid damage to the player.

local lol = require(game.ReplicatedStorage.Zone)

local part = lol.new(script.Parent)

playerEntered = false

part.playerEntered:Connect(function(plr)
	playerEntered = true
	while playerEntered do
		script.Acid1:Play()
		plr.Character:FindFirstChild("Humanoid"):TakeDamage(10)
		task.wait(10)
		if playerEntered == false then
			break
		end
	end
end)

part.playerExited:Connect(function()
	playerEntered = false
end)


Making your playerEntered bool into a dictionary that stores players in the zone seems to fix it for me and would work better than a single global bool

local lol = require(game.ReplicatedStorage.Zone)

local part = lol.new(script.Parent)

playersInZone = {}

part.playerEntered:Connect(function(plr)
	playersInZone[plr] = true
	while playersInZone[plr] do
		script.Acid1:Play()
		plr.Character:FindFirstChild("Humanoid"):TakeDamage(10)
		task.wait(10)
	end
end)

part.playerExited:Connect(function(plr)
	playersInZone[plr] = nil
end)

Though your issue is that when the players touch the part it immediately deals damage since that is first in the loop. Have the wait be first, then have an if statement to see if the player is still in the zone, and if they are, damage them

Issue still persists.β€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Ž

Refer to the text below the code, should’ve worded what I had done better. I made your boolean into a dictionary so this works for multiple players rather than for only one

Most likely your issue is the ordering of the loop itself, you deal the damage then wait the 10 seconds when it should wait first then damage because that’s most likely what you assume is it making the loop multiple times

local lol = require(game.ReplicatedStorage.Zone)

local part = lol.new(script.Parent)

playersInZone = {}

part.playerEntered:Connect(function(plr)
	playersInZone[plr] = true
	while playersInZone[plr] do
		task.wait(10)
		if not playersInZone[plr] then
			break
		end
		plr.Character:FindFirstChild("Humanoid"):TakeDamage(10)
		script.Acid1:Play()
	end
end)

part.playerExited:Connect(function(plr)
	playersInZone[plr] = nil
end)
1 Like

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