Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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)

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.