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

Why isn't this debounce working?

Asked by
neoG457 315 Moderation Voter
9 years ago
local enabled = true

x.Touched:connect(function(Hit)
    if not enabled then

enabled = true

    local HitPlayer = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if HitPlayer and HitPlayer ~= player and Hit.Parent:FindFirstChild("Humanoid") then
        Hit.Parent.Humanoid:TakeDamage(20) 


end
    end)

wait(10)

enabled = false
0
Oh look somebody downvotes for no reason :D neoG457 315 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

You actually have a couple things wrong here.

In the Touch event you have 2 if statements and only 1 end, along with the variable enabled being set to true on Line 1. Here is a cleaner and fixed version of your code.

local enabled = false

x.Touched:connect(function(Hit)
    if not enabled then
        enabled = true
        local HitPlayer = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if HitPlayer and HitPlayer ~= player and Hit.Parent:FindFirstChild("Humanoid") then
            Hit.Parent.Humanoid:TakeDamage(20)
             wait(10)
            enabled = false
        end
    end
end)
0
Thanks. neoG457 315 — 9y
Ad
Log in to vote
0
Answered by
yumtaste 476 Moderation Voter
9 years ago

The enabled value starts at true. On line 4, the conditional statement only allows the code below it fire if enabled is false. Just change enabled to false in the beginning and it will work.

Answer this question