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)
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
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)
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
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.
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)