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

sword still damaging even though debounce is true?

Asked by 1 year ago

Hi, so basically im making an ability for a sword where it does multiple slashes continuously. For some reason though, even after i did the if statement for debounce, and set it to true, it still damages the player? I would really appreciate help. Thanks!

Heres the script in serverscript service that gets activated from a remote event:

game.ReplicatedStorage.standinghere.OnServerEvent:Connect(function(player)
    local debounce1 = false
    local debounce2 = false
    local debounce3 = false
    local char = player.Character
    local sword = player.Character:FindFirstChild("HF-Blade")

    wait(0.5)

    sword.Handle.hitbox.Touched:Connect(function(Hit)
        if Hit.Parent:FindFirstChild("Humanoid") then
        if debounce1 == false then
                debounce1 = true
                Hit.Parent.HumanoidRootPart.Anchored = true
                local hp = game.Players:GetPlayerFromCharacter(Hit.Parent)
                local sw = Hit.Parent:FindFirstChildOfClass("Tool") or hp.Backpack:FindFirstChildOfClass("Tool")
                if sw:FindFirstChild("Script") then
                    sw.Script.Disabled = true
                end
                if sw:FindFirstChild("Ability") then
                    sw.Ability.Disabled = true
                end
                game.ReplicatedStorage.Bump:FireAllClients()
            sword.Handle.hitt:Play()
            local anim = Instance.new("Animation")
            anim.AnimationId = "https://www.roblox.com/asset/?id=10215173320"
            local playAnim = char.Humanoid:LoadAnimation(anim)
            playAnim:Play()
            wait(0.4)
            local a = sword.Handle.OrangeSpark:Clone()
            local b = sword.Handle.WhiteSpark:Clone()
            local c = sword.Handle.Wisps:Clone()
            a.Parent = Hit.Parent.HumanoidRootPart
            b.Parent = Hit.Parent.HumanoidRootPart
            c.Parent = Hit.Parent.HumanoidRootPart
            a.Enabled = true
            b.Enabled = true
            c.Enabled = true
            sword.Handle.raidensword:Play()
                sword.Handle.Touched:Connect(function(hit) -- The part that still damages
                    if hit.Parent:FindFirstChild("Humanoid") then
                if debounce2 == false then
                    debounce2 = true
                    sword.Handle.ow:Play()
                    hit.Parent.Humanoid:TakeDamage(dph)
                    wait(.1)
                    debounce2 = false
                end
                    end

                end)
                wait(4.22)
                debounce2 = true
-- not important below
                a.Enabled = false
                b.Enabled = false
                c.Enabled = false
                sword.Handle.raidensword:Stop()
                sword.Handle.PLAYTIME:Play()
                wait(1.4)
                sword.Handle.STAB:Play()
                sword.Handle.Touched:Connect(function(hit)
                    if hit.Parent:FindFirstChild("Humanoid") then
                    if debounce3 == false then
                        debounce3 = true
                            hit.Parent.Humanoid:TakeDamage(stabdmg)
                            game.ReplicatedStorage.Boom:FireAllClients()
                            a.Enabled = true
                            b.Enabled = true
                            c.Enabled = true
                            wait(0.4)
                            a.Enabled = false
                            b.Enabled = false
                            c.Enabled = false
                            wait(1)
                            a:Destroy()
                            b:Destroy()
                            c:Destroy()
                            end
                    end
                end)
            end
            end
        end)

end)

1 answer

Log in to vote
1
Answered by
bdam3000 125
1 year ago

The problem is that you define the debounce in the function itself.

game.ReplicatedStorage.standinghere.OnServerEvent:Connect(function(player)
    local debounce1 = false
    local debounce2 = false
    local debounce3 = false

    -- Other code

end)

Since you placed them into the function, the debounce values become false every time you swing your sword.

Instead, define your debounce values outside of the function.

 local debounce1 = false
local debounce2 = false
local debounce3 = false

game.ReplicatedStorage.standinghere.OnServerEvent:Connect(function(player)

    -- Other code

end)

  • I also recommend disconnecting your .Touched event for better performance.
0
It works! Thank you so much! The only reason i didnt do it before is because the ability is meant to be used over and over again, but i just made it make the value true before it uses them, so thanks! Also, im still new to scripting so do you mind explaining what the Disconnect function does? DriBowser 55 — 1y
0
The disconnect function does as the name implies. It disconnects a connected event from running its connected function. You can read more about it here: https://developer.roblox.com/en-us/articles/events bdam3000 125 — 1y
0
Thanks! DriBowser 55 — 1y
Ad

Answer this question