Trying to make animation play for left and right arm and wait until the animation is finished before the other arm can play

i have a punching script. u press f, it punches left, then right. i want to wait until the left punch animation has finished before switching to the right punch animation and vice versa. i appreciate any help ty!!

client:

 local uis = game:GetService("UserInputService")
local p = game.Players.LocalPlayer
local c = p.Character or p.CharacterAdded:Wait()
local LeftPunch = false
local punchEvent = game.ReplicatedStorage:WaitForChild("punchEvent")
local punch_Animation = c:FindFirstChild("punch_Animation")

uis.InputBegan:Connect(function(inp, gameproc)
	if gameproc then return end
	if inp.KeyCode == Enum.KeyCode.F then
		--punch w left hand
		if not LeftPunch then
			punchEvent:FireServer("left")
			LeftPunch = true
		else
			--punch w right hand
			punchEvent:FireServer("right")
			LeftPunch = false
		end
	end
end)

server:

punchEvent.OnServerEvent:Connect(function(p: Player, arm)
	local c = p.Character
	if not c then return end
	local h: Humanoid = c:WaitForChild("Humanoid")
	local animator: Animator = h:WaitForChild("Animator")

	-- Function to start animation and update currentAnimation table
	local function playAnimation(animationId, armSide)
		punch_Animation.AnimationId = "rbxassetid://"..animationId
		local punchAnimTrack = animator:LoadAnimation(punch_Animation)
		punchAnimTrack.Priority = animations.priority
		punchAnimTrack:Play()
	end

	if arm == "left" then
		-- Start the left punch animation
		playAnimation(animations.left_punch2, "left")

	elseif arm == "right" then
		-- Start the right punch animation
		playAnimation(animations.right_punch2, "right")
	else
		return
	end
end)
AnimationTrack.Ended:Connect(function()

end)
1 Like

You could do something like this:

punchEvent.OnServerEvent:Connect(function(p: Player, arm)
	local c = p.Character
	if not c then return end
	local h: Humanoid = c:WaitForChild("Humanoid")
	local animator: Animator = h:WaitForChild("Animator")

	-- Function to start animation and update currentAnimation table
	local function playAnimation(animationId, armSide)
		punch_Animation.AnimationId = "rbxassetid://"..animationId
		local punchAnimTrack = animator:LoadAnimation(punch_Animation)
		punchAnimTrack.Priority = animations.priority
		punchAnimTrack:Play()
		return punchAnimTrack
	end

	if arm == "left" then
		-- Start the left punch animation
		local anim = playAnimation(animations.left_punch2, "left")
		anim.Ended:Wait()
		-- do what you want here
	elseif arm == "right" then
		-- Start the right punch animation
		local anim = playAnimation(animations.right_punch2, "right")
		anim.Ended:Wait()
		-- do what you want here
	else
		return
	end
end)
1 Like

hey man, thanks very much for your help. i had to do a few tweaks and stuff and .stopped was what i was looking for my bad but ty tho

hey king,
thanks a million for your help mate. stopped is what i was looking for mb and i had to do some tweaks to get the animations to wait for each arm thru the client and stuff but i appreciate the help tysm!!

solved it thanks guys.

client:

local uis = game:GetService("UserInputService")
local cas = game:GetService("ContextActionService")
local p = game.Players.LocalPlayer
local c = p.Character or p.CharacterAdded:Wait()
local AnimEnded = true
local currentPunch = 1
local punchEvent = game.ReplicatedStorage:WaitForChild("punchEvent")
local punch_Animation = c:FindFirstChild("punch_Animation")

local function checkAnimHasEnded(status)
	if status == "animation_ended" then
		AnimEnded = true
	end
end
punchEvent.OnClientEvent:Connect(checkAnimHasEnded)

uis.InputBegan:Connect(function(inp, gameproc)
	if gameproc then return end
	if inp.KeyCode == Enum.KeyCode.F then
		if AnimEnded then
			--punch w left hand
			if currentPunch == 1 then
				currentPunch = 2
				AnimEnded = false
				punchEvent:FireServer("left")
				--punch w right hand
			elseif currentPunch == 2 then
				currentPunch = 1
				AnimEnded = false
				punchEvent:FireServer("right")
			end
			else return
		end
	end
end)

server:

local punchEvent = game.ReplicatedStorage:WaitForChild("punchEvent")
local punch_Animation:Animation = nil
local punchAnimTrack = nil

local animations = {
	left_punch = 85784732358918,
	right_punch = 71124638747172,
	left_punch2 = 121277399345889,
	right_punch2 = 100759026510566,
	priority = Enum.AnimationPriority.Action4
}

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
		local function createPunchAnimation()
			punch_Animation = c:FindFirstChild("punch_Animation")
			if not punch_Animation then
				punch_Animation = Instance.new("Animation", c)
				punch_Animation.Name = "punch_Animation"
			end
			return punch_Animation
		end
		punch_Animation = createPunchAnimation()
	end)
end)

punchEvent.OnServerEvent:Connect(function(p: Player, arm)
	local c = p.Character
	local h: Humanoid = c:WaitForChild("Humanoid")
	local animator: Animator = h:WaitForChild("Animator")
	if not c then return end

	local function playAnimation(animationId, armSide)
		punch_Animation.AnimationId = "rbxassetid://"..animationId
		local punchAnimTrack = animator:LoadAnimation(punch_Animation)
		punchAnimTrack.Priority = animations.priority
		--punchAnimTrack:AdjustSpeed(1.2) --to adjust speed of the animations
		punchAnimTrack:Play()
		--tell client left/right animation has ended/stopped
			punchAnimTrack.Stopped:Connect(function()
				punchEvent:FireClient(p, "animation_ended")
			end)
		return punchAnimTrack
	end

	if arm == "left" then
		-- Start the left punch animation
		local punchAnimTrack = playAnimation(animations.left_punch2, "left")
	elseif arm == "right" then
		-- Start the right punch animation
		local punchAnimTrack = playAnimation(animations.right_punch2, "right")
	else
		return
	end
end)