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

Is debounce for this script working properly?

Asked by
Irunui 15
9 years ago
local debounce = false
local debounce2 = false
local toolNoning = humanoid:LoadAnimation(toolNoneAnim)
function attack(hit)
    if hit and hit.Parent then
        local enemy = hit.Parent:FindFirstChild("Humanoid")
        if enemy then
            if enemy:IsA("Humanoid") and hit.Parent.Name ~= sp.Name then
                debounce = true
                while debounce do
                    toolNoning:Play()
                    print "wait"
                    wait(3)
                    torso.TouchEnded:connect(function(hit2)
                        if hit2 == hit then
                            debounce = false
                        end
                    end)
                end
            end
        else
            if hit.CanCollide then
                humanoid.Jump = true
            end
        end
    end
end

Game tends to crash if many humanoid parts (such as a gun held by a humanoid with many parts) collide with it, but I'm fairly sure the debounce mechanism is supposed to stop that....

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

Creating a connection in a while loop will repeatedly generate the connection and they will not go away. This will crash your game. The connection should be made before the while loop, and disconnected when it's finished.

local debounce = false
local debounce2 = false
local toolNoning = humanoid:LoadAnimation(toolNoneAnim)
function attack(hit)
    if hit and hit.Parent then
        local enemy = hit.Parent:FindFirstChild("Humanoid")
        if enemy then
            if enemy:IsA("Humanoid") and hit.Parent.Name ~= sp.Name then
                debounce = true
                local connection;
                connection=torso.TouchEnded:connect(function(hit2)
                    if hit2 == hit then
                        debounce = false
                        connection:disconnect()
                    end
                end)
                while debounce do
                    toolNoning:Play()
                    print "wait"
                    wait(3)
                end
            end
        else
            if hit.CanCollide then
                humanoid.Jump = true
            end
        end
    end
end
0
So call"torso.TouchEnded:connect(function(hit2)" outside the while loop? Irunui 15 — 9y
0
btw, fan of your Laser Tanks game. Irunui 15 — 9y
0
Thank you so much! Irunui 15 — 9y
Ad

Answer this question