Trying to play an animation when you click the left mouse button. Though it won’t play.
(I get no error code)
local InputService = game:GetService("UserInputService")
local InputType = Enum.UserInputType
InputService.InputBegan:connect(function(input)
if input.UserInputType == InputType.MouseButton1 then
local Slash = Instance.new("Animation")
Slash.AnimationId = "rbxassetid://7117042416"
Slash:Play()
wait()
end
end)
Make sure you have the humanoid defined, reference what you need in variables like so:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
I changed animation to Slash, that works though I still get an error message…
Players.trueblockhead101.Backpack.Tool.Script:4: attempt to index nil with ‘Character’
I am going to show you the code so you know how to set it up, it’s better if you understand how to do this so next time you can properly create a similar script. Make sure to read all of the notes I made.
local tool = script.Parent
--This is where we will get the character, humanoid, etc. Referance all of it before we continue with the script.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local InputService = game:GetService("UserInputService")
local InputType = Enum.UserInputType
--Create 2 new variables, 1 for the animation, 1 for the slash animation.
local animation = nil
local slashAnimation = nil
--Each time the tool is equipped we create a new animation and load it to the humanoid.
tool.Equipped:Connect(function()
animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7117042416"
slashAnimation = humanoid:LoadAnimation(animation)
end)
--Destroy the animation and set slash to nil. We destroy the animation to remove it from the game's memory.
tool.Unequipped:Connect(function()
animation:Destroy()
slashAnimation = nil
end)
--Here we check if MB1 has been pressed and slash is not nil, that way we dont play an animation that doesnt exist.
local debounce = false
InputService.InputBegan:Connect(function(input, processed) --Processed basically means if the key is binded to a context action or if the player is typing
if input.UserInputType == InputType.MouseButton1 and slashAnimation and not processed then --And not processed means "If they aren't typing then..."
if debounce then return end --If the player is already swinging then dont proceed
debounce = true
slashAnimation:Play()
slashAnimation.Stopped:Wait() --Wait until the animation has finished
debounce = false
end
end)
Edited my code above, added a debounce so you cannot infinitely swing the sword. Make sure the swing animation has an animation priority of Action as well.
local InputService = game:GetService("UserInputService")
local InputType = Enum.UserInputType
local Player = game:GetService("Players").LocalPlayer
InputService.InputBegan:connect(function(input)
if input.UserInputType == InputType.MouseButton1 then
local Animation = Instance.new("Animation", Player.Character)
Animation.AnimationId = "rbxassetid://7117042416"
local Slash = Player.Character:FindFirstChild("Humanoid").Animator:LoadAnimation(Animation)
Slash:Play()
wait()
end
end)