How would I make a "timeout" for the attack animations?

This is the code I’ve got so far. I know its messy but just bare with me.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Humanoid = script.Parent.Parent:WaitForChild("Humanoid")
local s = script
local RootPart = Player.Character.HumanoidRootPart

local Animating = false
local Attacking = false

local Animdict = {
	[1] = s:WaitForChild("Animation1"),
	[2] = s:WaitForChild("Animation2"),
	[3] = s:WaitForChild("Animation3"),
	[4] = s:WaitForChild("Animation4"),
	[5] = s:WaitForChild("IdleAttacking"),
}

local CURRENTVAL = Instance.new("NumberValue", s)
CURRENTVAL.Value = 1

local Animating = false
Mouse.Button1Down:Connect(function()
	if not Attacking then	
		Attacking = true
		Humanoid:LoadAnimation(Animdict[5]):Play()
	end
	if not Animating then 
		Animating = true	
			Humanoid.JumpPower = 0
			Humanoid:LoadAnimation(Animdict[CURRENTVAL.Value]):Play()	
				--//Movement
			BV = Instance.new("BodyVelocity", RootPart)
			BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			BV.Velocity = RootPart.CFrame.lookVector.Unit * 4
				--// Sound Effects
			swing = Instance.new("Sound",RootPart)
			swing.SoundId = "rbxassetid://4374877983"
			swing.PlaybackSpeed = 2
			swing:Play()		
			swing.TimePosition = 0.1000000000000000111
				CURRENTVAL.Value = CURRENTVAL.Value + 1
		if CURRENTVAL.Value > 2 then
			CURRENTVAL.Value = 1
		end	
		if CURRENTVAL.Value == 1 then
			wait(.33)
		elseif CURRENTVAL.Value == 2 then
			wait(.33)
		elseif CURRENTVAL.Value == 3 then
			wait(.33)
		elseif CURRENTVAL.Value == 4 then
			wait(.33)
		end
		Animating = false
		BV:Destroy()
		swing:Stop()
		swing:Destroy()
	end
end)

I saw this- How would I go about creating animation combos for melee weapons? - #6 by EmeraldSlash but I do not know how I’d add this to my current script
Here’s a quick demo of what it currently does :

Any help would be greatly appreciated!

2 Likes

So do you mean a delay every time for the attack animations?

1 Like

No if I clicked and waited to long the Value would reset back to 1 besides staying at 2.

I figured it out, so at the beginning or the start of your script, add a variable named timeout.

local timeout = 0

Then what you do next is, at the beginning of the mouse button1 function, and before the line of if not attack, you will set the timeout to 0 for every time if you hit the animation for the while wait() do which will handle it.

timeout = 0

The last thing you will do is adding a while wait() do. Which is the most important part. In a while wait() do must be at the end of the script, otherwise it will only loop and cannot continue to the next line of the script.

while wait(.1) do -- while wait do for timeout
	if Attacking == true then -- detecting if attacking
		timeout = timeout + .1 -- add a timeout
		--print(timeout) not really necessary if it's fixed
		if timeout > 1 then -- if the timeout is larger than 1 then
			timeout = 0 -- set it to 0
			CURRENTVAL.Value = 1 -- set it back to 1
			Animating = false -- set to false, and BV destroyed.
			Attacking = false
			BV:Destroy()
			swing:Stop()
			swing:Destroy()
		end
	end

end

Somewhere in line 46, you don’t need this. (Even Animating = false in the mouse button1 function)

if CURRENTVAL.Value == 1 then
			wait(.33)
		elseif CURRENTVAL.Value == 2 then
			wait(.33)
		elseif CURRENTVAL.Value == 3 then
			wait(.33)
		elseif CURRENTVAL.Value == 4 then
			wait(.33)
		end

The line in mouse button1, this is not necessary because we had moved to the while wait do.

BV:Destroy()
swing:Stop()
swing:Destroy()

Full script:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Humanoid = Player.Character:WaitForChild("Humanoid")
local s = script
local RootPart = Player.Character.HumanoidRootPart

local Animating = false
local Attacking = false

local Animdict = {
	[1] = s:WaitForChild("Animation1"),
	[2] = s:WaitForChild("Animation2"),
	[3] = s:WaitForChild("Animation3"),
	[4] = s:WaitForChild("Animation4"),
	[5] = s:WaitForChild("IdleAttacking"),
}

local CURRENTVAL = Instance.new("NumberValue", s)
CURRENTVAL.Value = 1

local Animating = false
Mouse.Button1Down:Connect(function()
	if not Attacking then	
		Attacking = true
		Humanoid:LoadAnimation(Animdict[5]):Play()
	end
	if not Animating then 
		Animating = true	
		Humanoid.JumpPower = 0
		Humanoid:LoadAnimation(Animdict[CURRENTVAL.Value]):Play()	
		--//Movement
		BV = Instance.new("BodyVelocity", RootPart)
		BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		BV.Velocity = RootPart.CFrame.lookVector.Unit * 4
		--// Sound Effects
		swing = Instance.new("Sound",RootPart)
		swing.SoundId = "rbxassetid://4374877983"
		swing.PlaybackSpeed = 2
		swing:Play()		
		swing.TimePosition = 0.1000000000000000111
		CURRENTVAL.Value = CURRENTVAL.Value + 1
		if CURRENTVAL.Value > 2 then
			CURRENTVAL.Value = 1
		end	
		if CURRENTVAL.Value == 1 then
			wait(.33)
		elseif CURRENTVAL.Value == 2 then
			wait(.33)
		elseif CURRENTVAL.Value == 3 then
			wait(.33)
		elseif CURRENTVAL.Value == 4 then
			wait(.33)
		end
		Animating = false
		BV:Destroy()
		swing:Stop()
		swing:Destroy()
	end
end)
1 Like

If there’s a problem, reply to me. I was rushing.

Edit: I don’t really have time right now so if there’s a problem with the full script, just do the steps followed by me (the while wait do) so find the problem by yourself.

1 Like

Not a scripter, and I know this method is not practical for most scripts, but since me lacking the knowledge needed for making Tool Use cool downs and such, I just put script.Disabled = true then do

wait(-- any time length you want would go here) --> wait(5)

Then I’d just do script.Disabled = false.

Not exactly sure if this helps, but it does work for me in some situations, although it’s not very practical.

1 Like

w h a t. I’d just like to say this is a model, not a tool. I did that so the handle isnlt forced stuck to the player.

First, your just repeatedly resetting the value to 1, so even if I clicked to change the value it will be reset back to 1 before the 2nd animation could have a chance to play.
Second the code on line 46 is very necessary because all my animations for the combo differ from the last. I wouldn’t want my animations playing over each other.

I do thank you for the time you’ve spent though!

Oh sorry if that didn’t work because your script was indeed messy. But what you can just do is adding a timeout, and adding a while wait(.1), (script at last message)