Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Any way to put a simple debounce in this script?

Asked by 5 years ago

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)
0
use a server-side script Wicked_Wlzard 110 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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

Answer this question