Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My animation does damage even after the animation?

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

✓ Accepted answer

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)

1 comment

0

Thank you for the help I understand now and I accept this answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.