For some reason my animation doesn't work

here is an error code " Play is not a valid member of Animation “Workspace.Yo2uber99.Tool.Animation”’"

here is the script :

script.Parent.Equipped:Connect(function()
	print("1")

	local player = game:GetService("Players").LocalPlayer

	print("2")

	local character = script.Parent.Parent

	print("3")

	local animation = script.Parent:FindFirstChild("Animation")

	print("4")

	local Humanoid = character:WaitForChild("Humanoid")
	
	local animator = animation
	
	local animator = Humanoid

	local canSwing = true

	local debounce = 1

	print("hi")

	script.Parent.Activated:Connect(function()

		if canSwing then

			print("yeet")

			canSwing = false

			animation:Play()

			print("ok")

			wait(debounce)

			canSwing = true

		end

	end)
end)
1 Like

Hello @Yo2uber99

Follow the instruction on This page and use the animatior

Roblox Dev API: Animator | Roblox Creator Documentation

1 Like

attempt to index nil usually means that you’ve just tried to get or call a child of an instance that doesn’t exist. In this case, it might simply be because it tried to run while it was in the backpack, but it errored as script.Parent.Parent would be game.Players. Try defining the character as player.Character instead of script.Parent.Parent.

new error “LoadAnimation is not a valid member of Animation “Workspace.Yo2uber99.Tool.Animation”” for the new code line :

local player = game:GetService("Players").LocalPlayer

	print("2")

	local character = script.Parent.Parent

	print("3")

	local animation = script.Parent:FindFirstChild("Animation")

	print("4")

	local Humanoid = character:FindFirstChild("Humanoid")
	
	local animator = animation
	
	local animationTrack = animator:LoadAnimation(animation)

Try this, it wont run if the humanoid isn’t there:

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = player:WaitForChild("Humanoid", 5)

if not humanoid then 
	warn("humanoid is missing")
	return nil 
else 
	print("humanoid found!")
end 

local tool = script.Parent 

local animation = tool:FindFirstChild("Animation")
local track = humanoid:LoadAnimation(animation)
local canSwing = true
local debounce = 1

tool.Activated:Connect(function()
	if canSwing then
		canSwing = false
		track:Play()
		task.wait(debounce)
		canSwing = true
	end
end)

Ok, so now you are trying to do LoadAnimation on an animation. There is no LoadAnimation function in animations. You probably want to do Humanoid:LoadAnimation() as the Humanoid does have a LoadAnimation function well technically it’s a method as it’s called with a colon :wink: so you want to use that.

In other words: local animator = Humanoid

edit: sorry for slight passive-aggressive tone

robloxapp-20211205-1029394.wmv (755.4 KB)

Instead of sending a video, try describing your issue.

I’m assuming that from this video you have checked the output and there are no errors. My guess is that because you are probably running this off a localscript, the animation won’t load. So you will have to do some work with remoteevents

(in case you are running the code in a script, try switching to a localscript)

every time i click my mouse with the tool equiped the animation is supposed to play but it doesn’t and keeps giving me errors that don’t make any sense at all.

I’ll be 100% more helpful than other peoples here, when you do character:FindFirstChild(“Humanoid”). It can return nil, so that’s why it says that nil doesn’t have LoadAnimation.
Instead do character:WaitForChild(“Humanoid”), it will wait until Humanoid exists in the character.

The difference between WaitForChild and FindFirstChild is that WaitForChild will wait until Humanoid exists. And FindFirstChild checks if something exist or else it return nil.

Make sure that the script parenting is correct.
If you want the get the player’s character with a localscript do game.Players.LocalPlayer.Character instead of script.Parent.Parent

What errors are you getting? You have to tell us what errors otherwise we can’t help.

  1. not sure how thats 100% more helpful than us
  2. the script does check if humanoid is nil

What are the errors saying? more chars

Try this:

local Tool = script.Parent
local Animation = script.Animation
local Debounce = false

Tool.Equipped:Connect(function()
	local Humanoid = script.Parent.Parent:WaitForChild("Humanoid", 0.1)
	if not Humanoid then
		warn("There is no humanoid!")
		return
	else
		local AnimationTrack = Humanoid:LoadAnimation(Animation)
		Tool.Activated:Connect(function()
			if Debounce then return end
			AnimationTrack:Play()
			Debounce = true
			wait(1)
			Debounce = false
		end)
	end
end)

Note: It’s important that you put the items like this, with the code being in the Script:
Capture
If you don’t, theres a 100% chance the script will break.