Help with making punch script Play by seperate clicks

Hello, here is what I’ve got.

local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local PunchAnim = Humanoid:LoadAnimation(script:WaitForChild("Punch1Anim"))
local PunchAnim2 = Humanoid:LoadAnimation(script:WaitForChild("Punch2Anim"))
local KickAnim = Humanoid:LoadAnimation(script:WaitForChild("Kick1Anim"))

local Cooldown = false

Mouse.Button1Down:Connect(function()
	if not Cooldown then
		Cooldown = true

		PunchAnim:Play()
		wait(0.8)
		
	
		
		PunchAnim2:Play()
			wait(0.8)
		
		
		
		KickAnim:Play()
				wait(0.8)
				
		Cooldown = false
	end
	end)

Its working just I want it to require a seperate click for the 3 anims, but its playing the whole thing after one click. Could someone help? :slight_smile:

2 Likes

You could make a variable that stores which animation needs to be played next, or you could make it use math.random to pick a random animation to play. If you want the animations to play in the correct order, you would use a variable. Here’s a script for that:

local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local PunchAnim = Humanoid:LoadAnimation(script:WaitForChild("Punch1Anim"))
local PunchAnim2 = Humanoid:LoadAnimation(script:WaitForChild("Punch2Anim"))
local KickAnim = Humanoid:LoadAnimation(script:WaitForChild("Kick1Anim"))

local NextAnim = 1 --Stores which animation should play next
local Cooldown = false

Mouse.Button1Down:Connect(function()
	if not Cooldown then
		Cooldown = true

		if NextAnim == 1 then
			NextAnim = 2
			PunchAnim:Play()
			wait(0.8)
		elseif NextAnim == 2 then
			NextAnim = 3
			PunchAnim2:Play()
			wait(0.8)
		elseif NextAnim == 3 then
			NextAnim = 1
			KickAnim:Play()
			wait(0.8)
		end
	
		Cooldown = false
	end
end)
1 Like

You could try a math.random to seperate them by different clicks.

1 Like

Not sure how to do Math.Random could you give an example?

Ty I’m trying your changes right now, its telling me that for this line
NextAnim == 2
Incomplete statement:Expected assignment or a function call. What do I do?

I’m not sure I’m understanding what you want. Are you asking for like combos for clicking and then if the player doesn’t click fast enough the combo resets to the initial move?

Pretty much.

Character limit–__________________________

Oops, I meant to put one equal sign instead of two. I edited my original reply with a fix.

1 Like

Oh sorry math.random can be used like:

if math.random(1,2) == 1 then
Punch1Anim:Play()
elseif math.random(1,2) == 2 then
Punch2Anim:Play()
end

What it does is makes it so on different clicks it can play different animations or do different things other than anims.

2 Likes

If you want a math.random script which plays each animation in a random order, here’s one:

local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local PunchAnim = Humanoid:LoadAnimation(script:WaitForChild("Punch1Anim"))
local PunchAnim2 = Humanoid:LoadAnimation(script:WaitForChild("Punch2Anim"))
local KickAnim = Humanoid:LoadAnimation(script:WaitForChild("Kick1Anim"))

local NextAnim = math.random(1,3) --Stores which animation should play next
local Cooldown = false

Mouse.Button1Down:Connect(function()
	if not Cooldown then
		Cooldown = true

		if NextAnim == 1 then
			NextAnim = math.random(1,3)
			PunchAnim:Play()
			wait(0.8)
		elseif NextAnim == 2 then
			NextAnim = math.random(1,3)
			PunchAnim2:Play()
			wait(0.8)
		elseif NextAnim == 3 then
			NextAnim = math.random(1,3)
			KickAnim:Play()
			wait(0.8)
		end
	
		Cooldown = false
	end
end)
1 Like

Oh haha, Ill try it again like that.

Update:
its working now! :slight_smile: but I’ve ran into two issues, would appreciate it if you could help me.
how would I make it so that if They dont press the second click fast enough, lets say in a 0.5 second window the combo would end and they’d have to do the first hit again cuz it seems even if i wait 10 seconds i just lets me do the second hit again.

Also another issue is while the anim is playing if you click again it gets interrupted and starts playing that anim again, any ideas?—
never mind ignore this part I fixed it. Just need help with the first question

After you set the cooldown back to false, you would wait for .5 seconds and see if NextAnim changed. If it didn’t change, then the player still hasn’t clicked so you should set NextAnim back to 1, so the combo restarts.
Code:

local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local PunchAnim = Humanoid:LoadAnimation(script:WaitForChild("Punch1Anim"))
local PunchAnim2 = Humanoid:LoadAnimation(script:WaitForChild("Punch2Anim"))
local KickAnim = Humanoid:LoadAnimation(script:WaitForChild("Kick1Anim"))

local NextAnim = math.random(1,3) --Stores which animation should play next
local Cooldown = false

Mouse.Button1Down:Connect(function()
	if not Cooldown then
		Cooldown = true

		if NextAnim == 1 then
			NextAnim = math.random(1,3)
			PunchAnim:Play()
			wait(0.8)
		elseif NextAnim == 2 then
			NextAnim = math.random(1,3)
			PunchAnim2:Play()
			wait(0.8)
		elseif NextAnim == 3 then
			NextAnim = math.random(1,3)
			KickAnim:Play()
			wait(0.8)
		end
		
		local CurrentNextAnim = NextAnim --Stores the animation that currently should play next.
		Cooldown = false
		
		wait(.5)
		if CurrentNextAnim == NextAnim then --If the player still hasn't clicked (which would change NextAnim's value) then
			NextAnim = 1 --End the combo by setting the animation back to the first one.
		end
	end
end)
1 Like

ty so much! it worked, you’re a genius. This was a really good learning moment, I’m sure this info will come in handy in many future projects.

1 Like

So here is what I came up with, and I have a friend who did it in a similar way but I just store the moves and do them all at once with a reasonable delay between all of them and then apply the cool down in between each combo.

local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local fightingMoves = {"Punch1", "Punch2", "Punch3", "Punch4"}

--// Variables
local currentMove = 1
local coolDownTime = 0.8
local comboTime = 0.8
local moveCache = {} --> Stores the moves the player is going to be doing.
local cooldown = false

--// Functions
local function DoAction()
	print("STARTING")
	local lastLog = tick()
	
	--// Wait
	repeat
		wait()
	until (tick() - lastLog) > 0.3
	
	--// Check
	if not (cooldown) then
		cooldown = true
		for _, move in pairs(moveCache) do
			print(move)
			wait(comboTime)
		end
		
		--// Reset
		wait(coolDownTime)
		moveCache = {}
		currentMove = 1
		cooldown = false
	else
		return
	end
end

local function StoreMove()
	local nextMove = fightingMoves[currentMove]

	--// Check
	if (nextMove) then
		table.insert(moveCache, nextMove)
		currentMove = currentMove + 1
		
		--// Check
		if (currentMove > #fightingMoves) then
			return
		end
		
		DoAction()
	end
end

Mouse.Button1Down:Connect(function()
	StoreMove()
end)