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

Any way to put a simple debounce in this script?

I just need a simple debounce of 2 seconds, i tried to do it myself but i'm a bit special so yea

local tool = script.Parent.Parent --your tool
local toolActive = false
local damage = 1
local duration = 2

local function onTouched(hit)
    if not toolActive then do return end end
    local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    if Humanoid then
        Humanoid:TakeDamage(damage)
    end
end

local function onActivated()
    if toolActive then do return end end
    toolActive = true; wait(duration); toolActive = false
end

script.Parent.Touched:Connect(onTouched)
tool.Activated:Connect(onActivated)

1 comment

0

use a server-side script

1 answer

✓ Accepted answer

You can use tables to identify if the target player was damaged or not.

local tool = script.Parent.Parent --your tool
local toolActive = false
local damage = 1
local duration = 2
local damagedChars = {}

local function checkIfDamaged(x)
	for index, value in pairs(damagedChars) do
		if value == x then return true end
	end
	return false
end

local function onTouched(hit)
	if not toolActive then do return end end
	local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if Humanoid and not checkIfDamaged(Humanoid.Parent.Name) then
		Humanoid:TakeDamage(damage)
		table.insert(damagedChars, Humanoid.Parent.Name)
	end
end

local function onActivated()
	if toolActive then do return end end
	toolActive = true; wait(duration); toolActive = false
	damagedChars = {}
end

script.Parent.Touched:Connect(onTouched)
tool.Activated:Connect(onActivated)

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