Animation won't play (Solution found)

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)

1 Like

You need to load the animation

Animation = humanoid:LoadAnimation(Slash)
Animation:Play()

o, my bad lol


1 Like

Though, humanoid and Animation (in the first line) have a red line underneath them. And when I test it the animation won’t load :confused:

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")

Also, change Animation to Slash.

1 Like

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’

Any ideas on what is wrong?


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)

Still won’t work. line 5 is bugged… Players.trueblockhead101.Backpack.Tool.Script:5: attempt to index nil with ‘Character’

This code does not error in my studio, I am using a local script. If you are using a server script then this will most likely throw an error.

Alright it works, thanks :smiley: Though it isn’t playing the full animation, I’ll check if it’s an error with the animation first.

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.

oh ok. Debounce is when you have to wait for something to Finnish before you can run the code again right?

you have to load the animation into the humanoid.

heres the right script:

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)
1 Like