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

My animation does damage even after the animation?

Asked by
ekweji 4
7 years ago

My problem is after the animation is done it can no longer do damage but it still does damage:

plr = game.Players.LocalPlayer
Kick = script:WaitForChild("KickAnimation")
Punch = script:WaitForChild("PunchAnimation")
mouse = plr:GetMouse()
RightArm = plr.Character:WaitForChild("Right Arm")
RightLeg = plr.Character:WaitForChild("Right Leg")
True = true
--> Punch Animation Script
mouse.KeyDown:connect(function(key)
    if key == "q" then
        if not True then return end
        True = false
        local aTrack = plr.Character.Humanoid:LoadAnimation(Punch)
        aTrack:Play()
        wait(2)
        True = true
        RightArm.Touched:connect(function(hit)
            local ehum = hit.Parent:FindFirstChild("Humanoid")
            if ehum and ehum ~= plr.Character.Humanoid then 
                ehum:TakeDamage(10)
                return hit

            end
        end)
    end
end)

1 answer

Log in to vote
1
Answered by 7 years ago

Your problem is here

        RightArm.Touched:connect(function(hit)
            local ehum = hit.Parent:FindFirstChild("Humanoid")
            if ehum and ehum ~= plr.Character.Humanoid then 
                ehum:TakeDamage(10)
                return hit

            end
        end)

The thing about events is that, when connected, they continue to work until the event is disconnected (using :disconnect()), the object that the event is child of (Ex. Right Arm) gets destroyed, or the script that the event is connected (this script) gets destroyed.

Also, do not use :connect() because it's old, use :Connect().

What you want to do, is to disconnect the Touched event.

First, we need to assign this event to a variable.

local touchedEvent = RightArm.Touched:Connect(function(hit)
            local ehum = hit.Parent:FindFirstChild("Humanoid")
            if ehum and ehum ~= plr.Character.Humanoid then 
                ehum:TakeDamage(10)
                return hit

            end
        end)

Then after some time, disconnect the event.

touchedEvent:Disconnect()

After that, this script will no longer listen to the touched event.

plr = game.Players.LocalPlayer
Kick = script:WaitForChild("KickAnimation")
Punch = script:WaitForChild("PunchAnimation")
mouse = plr:GetMouse()
RightArm = plr.Character:WaitForChild("Right Arm")
RightLeg = plr.Character:WaitForChild("Right Leg")
True = true
--> Punch Animation Script
mouse.KeyDown:connect(function(key)
    if key == "q" then
        if not True then return end
        True = false
        local aTrack = plr.Character.Humanoid:LoadAnimation(Punch)
        aTrack:Play()
        local touchedEvent = RightArm.Touched:Connect(function(hit)
            local ehum = hit.Parent:FindFirstChild("Humanoid")
            if ehum and ehum ~= plr.Character.Humanoid then 
                ehum:TakeDamage(10)
                return hit

            end
        end)
        wait(2)
        True = true
        touchedEvent:Disconnect()
    end
end)
0
Thank you for the help I understand now and I accept this answer. ekweji 4 — 7y
Ad

Answer this question