Combat Combo Scripting Help

Hello Everyone,

I’m trying to write a simple combat script and some problems have occurred. I was able to get the character to punch once but when I try to make a combo, the animation loops itself (when I press F more than once).

[For GIF look below]

Can anyone diagnose the problem with this code?

Local Script:

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local punchEvent = RS:WaitForChild("PunchEvent")

local ready = true

local function punch(inputObject, gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.F and ready then
		punchEvent:FireServer()
		ready = false
		wait(0.723)
		ready = true
	end
end

UIS.InputBegan:Connect(punch)

Server Script:

local RS = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", RS)
punchEvent.Name = "PunchEvent"

local animations = {2907892447, 2914178057}
local isRight = true

local function onPunchFired(plr)
	local char = game.Workspace:FindFirstChild(plr.Name)
	local hum = char.Humanoid
	local animation = Instance.new("Animation")
	if isRight then
		animation.AnimationId = "rbxassetid://" ..animations[1]
		isRight = false
	else
		animation.AnimationId = "rbxassetid://" ..animations[2]
		isRight = true
	end
	local animTrack = hum:LoadAnimation(animation)
	animTrack.Looped = false
	animTrack:Play()	
	
end
	
punchEvent.OnServerEvent:Connect(onPunchFired)

At first I thought the animation was looped, so I set the looped property to false. That didn’t work. I’ve tried different ways of alternating the punches but nothing seems to work.

When I press F once:

When I press F twice (animation isn’t looped):

4 Likes

I don’t remember if this is possible, but try getting the length of the animation and waiting then using the :Stop() function.

1 Like

I re-imported the animation and now it works!

5 Likes