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:
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.
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)
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.
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)
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.
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
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.