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

Why is the damage and cooldown not working in my combat script?

Asked by 3 years ago

I created a combat script that has an animation. The animation works in the script, but I tried making it do damage and I also tried making a cooldown, but neither of those work. I'm able to spam the key instead of waiting before I can use it again. The damage also doesn't work because if I press the key and the animation isn't active, but I touch a humanoid it loses health. I just need help figuring out what I did wrong with the cooldown and the damage. This is the script:

game.ReplicatedStorage.Chop.OnServerEvent:Connect(function(plr)
    local rArm = plr.Character["Right Arm"]
    local debounce = false
    local hum = plr.Character["Humanoid"]
    local anim = Instance.new("Animation", workspace)
    anim.AnimationId = "rbxassetid://5821099556"
    local animplay = hum:LoadAnimation(anim)
    animplay:Play()
    rArm.Touched:Connect(function(hit)
        if not debounce then
            debounce = true
        if hit.Parent:FindFirstChild("Humanoid") then
            hit.Parent.Humanoid:TakeDamage(45)
                wait(5)
                debounce = false
            end
        end
    end)
end)
0
You have to put the local debounce at the very start of the script. Not inside the function. rabbi99 714 — 3y
0
When you don't have your tool out, it would still do damage through the arm because the event is still registered. rabbi99 714 — 3y
0
Ok I'll try that. KPNagai 4 — 3y
0
I moved local debounce at the start of the script, but I'm still able to spam the animation. KPNagai 4 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Alright so I messed up the first time but I think this should work now. (explanations in script)

local debounce = false -- debounce is outside the script
local debounce2 = false -- 2nd debounce for damage
local anim = Instance.new("Animation", workspace) -- animation is outside so you don't make a new instance every time you click
anim.AnimationId = "rbxassetid://5821099556" -- animationID

game.ReplicatedStorage.Chop.OnServerEvent:Connect(function(plr) -- on serverevent
    local rArm = plr.Character:WaitForChild("Right Arm") -- waiting for right arm
    local hum = plr.Character:WaitForChild("Humanoid") -- waiting for humanoid
    local animplay = hum:LoadAnimation(anim)
    if not debounce then -- if debounce == false then
        debounce = true
        debounce2 = false -- debounce2 is now false so damage can be used
        animplay:Play() -- playing animation
        wait(5) -- make sure this wait is how much time it will take for your animation to end
        debounce = false -- debounce = false
    end
    rArm.Touched:Connect(function(hit) -- when arm hits
        if not debounce2 and debounce then -- checking if animation is playing and if you're able to damage
            debounce2 = true -- now you can't damage anymore
            if hit.Parent:FindFirstChild("Humanoid") then -- checking if what you hit is a humanoid
                hit.Parent.Humanoid:TakeDamage(45) -- makes them take damage
            end
        end
    end)
end)
0
Thanks it works. KPNagai 4 — 3y
Ad

Answer this question