0

Why isn't my sword's debounce working?

Heyo, I'm trying to create a sword that takes 5 damage each time you click but it keeps taking 5 damage multiple times per click? How would I fix this?

local tool = script.Parent
local safe = true

local function OnTouch(partOther)
if safe == false then
local humanOther = partOther.Parent:FindFirstChild("NPC")
if not humanOther then return end
humanOther:TakeDamage(5)
end
end

tool.Handle.Touched:Connect(OnTouch)

script.Parent.Activated:Connect(function()  -- I thought this would work as a debounce?
safe = false
wait(1)
safe = true
end)


0
sO_Ov 195
3 days ago
Edited 3 days ago

Well, I suppose you are using a script, correct? I highly recommend you to use local script and make a communication between the client and the server using remote function, but anyway, this should work

local tool = script.Parent
local lastDamage = tick()
local waitTime = .6 -- debouncer

local function OnTouch(partOther)
if tick() - lastDamage >= waitTime then
lastDamage = tick()
local humanOther = partOther.Parent:FindFirstChild("NPC")
if not humanOther then return end
humanOther:TakeDamage(5)
end
end

tool.Handle.Touched:Connect(OnTouch)