Animations Are Wonky

For Some Reason the animation in my game are acting wonky

game.UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		punching = true
		if punchable == true then
			punchable = false
			if punch == 2 then
				if punching == true then
					punching = false
					if punching ~= true then
						local punch2anim = animator:LoadAnimation(script.Punch2)
						punch2anim.Looped = false
						punch2anim.Priority = Enum.AnimationPriority.Action2
						game.ReplicatedStorage.Punch:FireServer(LocalPlayer)
						punch2anim:Play()
						punch2anim:GetMarkerReachedSignal("HIT"):Connect(function()
							game.ReplicatedStorage.Punch:FireServer(LocalPlayer)
						end)
						print("a")
					end
				end
				task.wait(1.5)
				punchable = true
				punch = 1
			end
			if punch == 1 then
				if punching == true then
					punching = false
					if punching ~= true then
						local punch1anim = animator:LoadAnimation(script.Punch1)
						punch1anim.Looped = false
						punch1anim.Priority = Enum.AnimationPriority.Action
						punch1anim:Play()
						punch1anim:GetMarkerReachedSignal("HIT"):Connect(function()
							game.ReplicatedStorage.Punch:FireServer(LocalPlayer)
						end)
						print("ab")
					end
				end
				task.wait(1.5)
				punchable = true
				punch = 2
			end
		end
	end
end)

You Can See In The Video that The animations are sometimes choppy and sometimes the same animation plays twice in a row

1 Like

This should prevent duplicate animations from being played.

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local animator = Character:FindFirstChild("Humanoid"):FindFirstChild("Animator")

local punchable = true
local punch = 1
local punching = false

UserInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if not punchable or punching then
            return
        end

        punching = true
        punchable = false

        local anim
        if punch == 2 then
            anim = animator:LoadAnimation(script.Punch2)
            punch = 1
        else
            anim = animator:LoadAnimation(script.Punch1)
            punch = 2
        end

        anim.Looped = false
        anim.Priority = (punch == 2) and Enum.AnimationPriority.Action2 or Enum.AnimationPriority.Action
        anim:Play()

        -- Fire server event once for the current punch
        ReplicatedStorage.Punch:FireServer(LocalPlayer)

        -- Handle the HIT marker to fire server event again
        anim:GetMarkerReachedSignal("HIT"):Connect(function()
            ReplicatedStorage.Punch:FireServer(LocalPlayer)
        end)

        -- Wait for animation to finish
        anim.Stopped:Wait()

        -- Allow punching again after the animation completes
        punchable = true
        punching = false
    end
end)
1 Like

Thank You, Your Solution Worked!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.