Keybind Tool Animation Problem

I have a problem with my scripts. I’m making animations like blocking, counter-blocking, and slashing for my tool and for some reason it won’t load correctly. I have my animations stored inside of the tool:

Block - Roblox Studio 12_22_2021 5_25_27 PM (2)

Here’s my attack script:

local tool = script.Parent
local slash1 = tool.Slash1
local slash2 = tool.Slash2
local slash3 = tool.Slash3
local cooldown = false
local anim
local anim2
local anim3

tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
	local chosenSlash = math.random(1, 3)
		if chosenSlash == 1 then
			anim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(slash1)
			anim:Play()
		elseif chosenSlash == 2 then
			anim2 = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(slash2)
			anim2:Play()
		elseif chosenSlash == 3 then
			anim3 = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(slash3)
			anim3:Play()
		end
	end)
end)

tool.Unequipped:Connect(function()
	anim:Stop()
	anim2:Stop()
	anim3:Stop()
end)

My counter-block script:

local UIS = game:GetService("UserInputService")
local char = game.Players.LocalPlayer.Character

local Anim = script.Parent.CounterBlock

local keybind = Enum.KeyCode.E

local cancounter = true

UIS.InputBegan:Connect(function(input,gameprocessed)
	if gameprocessed then return end
	if not cancounter then return end
	
	if input.KeyCode == keybind then
		cancounter = false
		
		local playAnim = char.Humanoid:LoadAnimation(Anim)
		playAnim:Play()
		wait(.5)
		playAnim:Stop()
		wait(2)
		cancounter = true
	end
end)

And my Block script:

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local blockStart = script.Parent.BlockStart
local blockAnim = script.Parent.Block

local track
local track2

UIS.InputBegan:Connect(function(input)
	if input.Keycode == Enum.KeyCode.F then
		track = Character.Humanoid:LoadAnimation(blockStart)
		track.Priority = Enum.AnimationPriority.Action
		track:Play()
		wait(.21)
		track:Stop()
		script.Parent.Parent.Humanoid.Walkspeed = 6
		track2 = Character.Humanoid:LoadAnimation(blockAnim)
		track2.Priority = Enum.AnimationPriority.Action
		track2.Looped = true
		track2:Play()
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.Keycode == Enum.KeyCode.F then
		track2:Stop()
		Character.Humanoid.Walkspeed = 16
	end
end)

As you see I’m not very good at handling animations yet, but I just do not know the problem at all.

1 Like

Maybe the animation isn’t loaded when you play it, try putting this line of code before playing.

while anim.Length == 0 and wait() do end
1 Like

Any errors part of this? Does it say something like Attempt to index nil with "Humanoid"?

It could be the fact that the player’s character was trying to load in.

No it says nothing about that, I did test right before I sent the message just to make sure that there were no error messages, and there weren’t any.

You seem to be doing everything correctly there, is there a chance you might have left those AnimationIds inside each Animation object empty?

Actually no since that would result in an error.

I thought everything was fine as well, but for some reason it just doesn’t work.

Do you have any idea of how to do this @B2ontwitch?

For scripts, I don’t recommend putting events in other events. Your first script’s code makes it so every time the player equips the tool it will create a new event which is not good.

Try this:

local tool = script.Parent
local slash1 = tool.Slash1
local slash2 = tool.Slash2
local slash3 = tool.Slash3
local cooldown = false
local anim
local anim2
local anim3
local Mouse = game.Players.LocalPlayer:GetMouse()
local tool_equipped = false

Mouse.Button1Down:Connect(function()
	if tool_equipped then
		local chosenSlash = math.random(1, 3)
		if chosenSlash == 1 then
			anim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(slash1)
			anim:Play()
		elseif chosenSlash == 2 then
			anim2 = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(slash2)
			anim2:Play()
		elseif chosenSlash == 3 then
			anim3 = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(slash3)
			anim3:Play()
		end
	end
end)

tool.Equipped:Connect(function(Mouse)
	tool_equipped = true
end)

tool.Unequipped:Connect(function()
	tool_equipped = false
	if anim then
		anim:Stop()
		anim = nil
	end
	if anim2 then
		anim2:Stop()
		anim2 = nil
	end
	if anim3 then
		anim3:Stop()
		anim3 = nil
	end
end)

For the second script, you’re making it so that every time a key on the keyboard is pressed down, the cancounter variable goes to false, which makes the anim not playable if they press another key.

Second script:

local UIS = game:GetService("UserInputService")

game.Players.LocalPlayer.CharacterAdded:Wait()

local Char = game.Players.LocalPlayer.Character
local Anim = script.Parent.CounterBlock
local keybind = Enum.KeyCode.E
local cancounter = true

UIS.InputBegan:Connect(function(input) -- I don't know what gameprocessed does so I deleted it.
	if input.KeyCode == keybind and cancounter then
		cancounter = false
		local playAnim = Char.Humanoid:LoadAnimation(Anim)
		playAnim:Play()
		wait(.5)
		playAnim:Stop()
		wait(2)
		cancounter = true
	end
end)

Third script:

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

Player.CharacterAdded:Wait()

local Character = Player.Character

Character:WaitForChild("Humanoid")

local blockStart = script.Parent.BlockStart
local blockAnim = script.Parent.Block
local track = nil
local track2 = nil
local debounce = false -- Make sure to add a debounce so the player can't play the same animation multiple times at once, which may cause bugs.

UIS.InputBegan:Connect(function(input)
	if input.Keycode == Enum.KeyCode.F and not debounce then
		debounce = true
		track = Character.Humanoid:LoadAnimation(blockStart)
		track.Priority = Enum.AnimationPriority.Action
		track:Play()
		wait(.21)
		if not debounce then
			track:Stop()
			Character.Humanoid.Walkspeed = 6
			track2 = Character.Humanoid:LoadAnimation(blockAnim)
			track2.Priority = Enum.AnimationPriority.Action
			track2.Looped = true
			track2:Play() 
		end
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.Keycode == Enum.KeyCode.F and debounce then
		if track then
			track:Stop() 
		end
		if track2 then
			track2:Stop()
		end
		track = nil
		track2 = nil
		Character.Humanoid.WalkSpeed = 16
		debounce = false
	end
end)
2 Likes

I get an error message that states, ‘Keycode is not a valid member of InputObject “InputObject”’, it also states it occurs on the lines, ‘17, and 35’ of the block script.

Those two lines were the ones that had ‘if input.Keycode’ inside of them.

Change the input.Keycode to input.KeyCode maybe

It fixed the block animation a little bit, the second looped animation still doesn’t play.

I accidently did something wrong in the third script. Try this:

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

Player.CharacterAdded:Wait()

local Character = Player.Character

Character:WaitForChild("Humanoid")

local blockStart = script.Parent.BlockStart
local blockAnim = script.Parent.Block
local track = nil
local track2 = nil
local debounce = false

UIS.InputBegan:Connect(function(input)
	if input.Keycode == Enum.KeyCode.F and not debounce then
		debounce = true
		track = Character.Humanoid:LoadAnimation(blockStart)
		track.Priority = Enum.AnimationPriority.Action
		track:Play()
		wait(.21)
		if not debounce then return end
		track:Stop()
		Character.Humanoid.Walkspeed = 6
		track2 = Character.Humanoid:LoadAnimation(blockAnim)
		track2.Priority = Enum.AnimationPriority.Action
		track2.Looped = true
		track2:Play() 
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.Keycode == Enum.KeyCode.F and debounce then
		debounce = false
		if track then
			track:Stop() 
		end
		if track2 then
			track2:Stop()
		end
		track = nil
		track2 = nil
		Character.Humanoid.WalkSpeed = 16
	end
end)
1 Like

Oh thanks I was just about to post this video:

https://gyazo.com/e7888aa82a7325831f5fe2606499be17

but I’ll test it out now, thanks.

For some reason it still did the same thing from last time, I changed the input.Keycode to input.KeyCode and it fixed the script a little bit but it still does the same thing.

Hmm… strange. Have you tried holding down the F key and waiting for the second animation to play? I can’t seem to think of anything else that might be wrong.

I found another error that is easy to fix. You said ‘Walkspeed’ instead of ‘WalkSpeed’. I’ll update you if anything happens after I fix it

I FOUND THE ISSUE!! It’s the fact I have the LocalScript’s name as Block, and the Animation called Block aswell.

1 Like

Nice. The same thing happened to be lol. I used a screen gui called Chat and my code was glitching because Roblox automatically puts a screen gui that is called Chat and my script was getting confused between the 2

Alright the Block animation works now, but it is very buggy, I cant think of a way to make this not happen:

https://gyazo.com/604a58e44803983ab84bf9e09125b2f4

The other scripts still don’t work for some reason and I am currently trying to make it so that the player cannot run while blocking, and the animation is slower.