I am trying to make a sword for my game that will allow my character to block left when the q button is pressed but still play a animation to swing the sword. I haven't been scripting for long so it would be nice if you could point out all errors I have. This is the code I currently am working with. If you need any other scripts that I am working with to understand the problem I will gladly show you them. Thank you, -Peanut
local function WaitForChild(parent, childName)
while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
return parent[childName]
end
local Tool = script.Parent
local Animations = {}
local MyHumanoid
local MyCharacter
local function PlayAnimation(animationName)
if Animations[animationName] then
Animations[animationName]:Play()
end
end
local function StopAnimation(animationName)
if Animations[animationName] then
Animations[animationName]:Stop()
end
end
function OnEquipped(mouse)
MyCharacter = Tool.Parent
MyHumanoid = WaitForChild(MyCharacter, 'Humanoid')
if MyHumanoid then
Animations['EquipAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'EquipAnim5'))
Animations['IdleAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'IdleAnim3'))
Animations['OverheadAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'OverheadAnim2'))
Animations['SlashAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'SlashAnim2'))
Animations['ThrustAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'ThrustAnim2'))
Animations['UnequipAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'UnequipAnim2'))
Animations['Blockleft'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'Blockleft'))
end
PlayAnimation('EquipAnim')
PlayAnimation('IdleAnim')
end
function Blockleft(key)
PlayAnimation('Blockleft')
end
function OnUnequipped()
for animName, _ in pairs(Animations) do
StopAnimation(animName)
end
end
mouse.Keydown:connect(function(key))
if key == "q" then
Tool.Equipped:connect('BlockLeft')
end
Tool.Equipped:connect(OnEquipped)
Tool.Unequipped:connect(OnUnequipped)
WaitForChild(Tool, 'PlaySlash').Changed:connect(
function (value)
PlayAnimation('SlashAnim')
end)
WaitForChild(Tool, 'PlayThrust').Changed:connect(
function (value)
PlayAnimation('ThrustAnim')
end)
WaitForChild(Tool, 'PlayOverhead').Changed:connect(
function (value)
PlayAnimation('OverheadAnim')
end)

2 comments
Are you making a Star Wars Battle Front game.. if you are I know who you are :)
I am going to be making something close to that latter. It will have ground battles with clones vs droids while there are space battles going on overhead, but I won't be working on that till I am done with the game I am working on right now which is a RPG made around the anime of SAO (Sword Art Online) I have no idea when it will be out fully and I will start on the star wars war thing after I am