Dead Zone doesn't work

For my game, Im trying to make a Dead Zone so that players can’t camp in the water all of the time, so every second their health will deplete by 20, and when they get out then their health will stop depleting. For some reason this doesn’t work at all.

script.Parent.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:WaitForChild("Humanoid")
	for i = 1,5 do
		Humanoid.Health = Humanoid.Health - 20
	end
end)

their are no errors.

1 Like

This function is triggered when they touch the water and the loop will continue to repeat until its completed, it will not be canceled if the player stops touching the water.

1 Like

then what should I do to fix this?

You can try this script, I hope it helps!

local db = false
script.Parent.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:WaitForChild("Humanoid")
	if db == false then
        Humanoid.Health = Humanoid.Health - 20
       db = true
    end
    wait(0.1) -- Ammount of time to wait until player gets damaged again
    db = false
end)

how is this going to help??? its just basically another wait/pause

nvm figure out how to use the correct format for lua posts try this out

local InZone = {}

script.Parent.Touched:Connect(function(hit)
	local Hum = hit.Parent:FindFirstChild("Humanoid")
	if Hum then
		local InTable = table.find(InZone,hit.Parent)
		if not InTable then
			table.insert(InZone,#InZone+1,hit.Parent)
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local Hum = hit.Parent:FindFirstChild("Humanoid")
	if Hum then
		local InTable = table.find(InZone,hit.Parent)
		if InTable then
			table.remove(InZone,InTable)
		end
	end
end)

while wait(1) do
	for i,v in ipairs(InZone) do
		local Hum = v:FindFirstChild("Humanoid")
		if v ~= nil and Hum then
			print("Damage")
			Hum:TakeDamage(20)
		else
			table.remove(InZone,i)
		end
	end
end