Trying to make a damage zone (Help)

So im recently trying to make a damage zone for a mechanic in my game but i just cant get it to work but i have no idea why its not working… It deals damage once or twice and then stops to do so.

local db = true

local function touched(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		print("Touched")
		if db  then
			db = false
			local hum = hit.Parent.Humanoid
			
			hum:TakeDamage(5)
			
			wait(1)
			db = true
		end
	end
end

script.Parent.Touched:Connect(touched)
2 Likes

can you try printing was “hit” is first

Are you trying to get it to keep damaging the player 5 health every second?

Yes im trying so that every second the player is inside the area it takes damage.

It already does by printing touched but it only prints once or twice instead of constantly doing it like i want it to…

assuming this is a local script? if not, the way you’re handling the touch debounce is incorrect.
you have to use an array to control the debounce.
if it is a local script try this:

	if hit.Parent:FindFirstChild("Humanoid") then
		if db  then
                        task.spawn(function()
                           task.wait(1)
                           db = true
                        end
			db = false
			local hum = hit.Parent.Humanoid
			hum:TakeDamage(5)
		end
	end
end

infinite damaging:

local RunService = game:GetService("RunService")

local damage = 5

local cooldown = 1
local currentTime = cooldown

local connection = nil

local function StopDamaging()
   if connection then
      connection:Disconnect()
   end
end

local function StartDamaging(character)
   StopDamaging()

   local humanoid = character:FindFirstChildOfClass("Humanoid")
   
   connection = RunService.Heartbeat:Connect(function()
      if tick() - currentTime >= cooldown and humanoid.Health > 0 then
         currentTime = tick()
         humanoid:TakeDamage(damage)
      end
   end)
end

local function OnTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		print("Touched")
        StartDamaging(hit.Parent)
	end
end

script.Parent.Touched:Connect(OnTouched)
script.Parent.TouchEnded:Connect(StopDamaging)
3 Likes

Yeah it was something like this that i was trying to do but i wasnt sure how to do it. Anyways thank. you so much!!! :wink:

1 Like

If you want all players, you can do this:

local part = script.Parent

local damage = 5
local cooldown = 1
local playersInZone = {}

local function OnTouchEnded(hit)
   local character = hit:FindFirstAncestorOfClass("Model")

   if character and table.find(playersInZone, character) then
      table.remove(playersInZone, table.find(playersInZone, character))
   end
end

local function OnTouched(hit)
    local character = hit:FindFirstAncestorOfClass("Model")

   if character and not table.find(playersInZone, character) then
      table.insert(playersInZone, character)
   end
end

part.Touched:Connect(OnTouched)
part.TouchEnded:Connect(OnTouchEnded)

while task.wait(cooldown) do
   for _, character in ipairs(playersInZone)
      local humanoid = character:FindFirstChildOfClass("Humanoid")

      if humanoid and humanoid.Health > 0 then
         humanoid:TakeDamage(damage)
      else
         table.remove(playersInZone, table.find(playersInZone, character))
      end
   end
end

What do you mean with if i want all players?

Players who step in the zone would be added to a list to be damaged every second and when a player leaves the zone or dies, they would be removed from the zone.

1 Like

I wouldn’t recommend using .Touched for a zone detection, I recently needed to do a zone detection to damage players and used ZonePlus - a much better and efficient alternative. It uses Roblox’s Spatial Query API and has little to no affects performance wise.

local container = workspace.AModelOfPartsRepresentingTheZone
local zone = Zone.new(container)

zone.playerEntered:Connect(function(player)
    print(("%s entered the zone!"):format(player.Name))
end)

zone.playerExited:Connect(function(player)
    print(("%s exited the zone!"):format(player.Name))
end)

2 Likes