Animation not playing on Local Script?

I want to be able to play the animation that I’ve created, but for some reason the Animation isn’t working. It’s annexing the humanoid as nil? . . but the player is clearly there? Is it not possible to perform animations from a local script, or is there something I’m missing?

I’ve tried to look on the devforum and have looked for the proper animation syntax on the Wiki, but neither seems to help.

-- Variables --
local UserInputService = game:GetService("UserInputService")
local RemoteEvent = game.ReplicatedStorage.HardWork
Player = game.Players.LocalPlayer
Character = Player.Character

-- Function --
UserInputService.InputBegan:connect(function(input)

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		RemoteEvent:FireServer()
		print ("Remote successfully fired.")
	end

	local Track = Instance.new("Animation")
	Track.AnimationId = "rbxassetid://6929015157"
	local Anim = Character.Humanoid:LoadAnimation(Track)
	Anim:Play()

end)

ERROR LIST

15:31:14.617 Players.hpgames16.PlayerScripts.Melee_Local:17: attempt to index nil with ‘Humanoid’ - Client - Melee_Local:17
15:31:14.618 Stack Begin - Studio
15:31:14.618 Script ‘Players.hpgames16.PlayerScripts.Melee_Local’, Line 17 - Studio - Melee_Local:17
15:31:14.619 Stack End - Studio

1 Like

Humanoid:LoadAnimation is deprecated,

try one. Character = Player:WaitForChild("Character")

Two: Local Anim = Character.Humanoid.Animator:LoadAnimation(Track)

I tried to do both, the first solution gave me an infinite yield, I don’t know why it’s not registering the character as nil, honestly. Here is the updated error list with the second attempt.

-- Variables --
local UserInputService = game:GetService("UserInputService")
local RemoteEvent = game.ReplicatedStorage.HardWork
Player = game.Players.LocalPlayer
Character = Player.Character

-- Function --
UserInputService.InputBegan:connect(function(input)

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		RemoteEvent:FireServer()
		print ("Remote successfully fired.")
	end

	local Track = Instance.new("Animation")
	Track.AnimationId = "rbxassetid://6929015157"
	local Anim = Character.Humanoid.Animator:LoadAnimation(Track)
	Anim:Play()

end)

ERROR LIST!

  15:44:06.087  Players.hpgames16.PlayerScripts.Melee_Local:17: attempt to index nil with 'Humanoid'  -  Client - Melee_Local:17
  15:44:06.087  Stack Begin  -  Studio
  15:44:06.088  Script 'Players.hpgames16.PlayerScripts.Melee_Local', Line 17  -  Studio - Melee_Local:17
  15:44:06.088  Stack End  -  Studio

Any suggestions?

1 Like
wait(1)

local UserInputService = game:GetService("UserInputService")
local RemoteEvent = game.ReplicatedStorage.HardWork
Player = game.Players.LocalPlayer
Character = Player.Character or Player.Character:Wait()
local Humanoid = Character.Humanoid

-- Function --
UserInputService.InputBegan:connect(function(input)

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		RemoteEvent:FireServer()
		print ("Remote successfully fired.")
	end

	local Track = Instance.new("Animation")
	Track.AnimationId = "rbxassetid://6929015157"
	local Anim = Humanoid.Animator:LoadAnimation(Track)
	Anim:Play()

end)

See if this works.

1 Like

Try this:

-- Variables --
local UserInputService = game:GetService("UserInputService")
local RemoteEvent = game.ReplicatedStorage.HardWork
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

-- Function --
UserInputService.InputBegan:connect(function(input)

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		RemoteEvent:FireServer()
		print ("Remote successfully fired.")
	end

	local Track = Instance.new("Animation")
	Track.AnimationId = "rbxassetid://6929015157"
	local Anim = hum.Animator:LoadAnimation(Track)
	Anim:Play()
end)
1 Like

OH! Did you ever parent the Track?

1 Like

Did some testing, and it seems to be a problem with the character variable, not anything else. Trying to figure out more.

1 Like
  15:49:44.427  Players.hpgames16.PlayerScripts.Melee_Local:7: attempt to index nil with 'Wait'  -  Client - Melee_Local:7
  15:49:44.428  Stack Begin  -  Studio
  15:49:44.428  Script 'Players.hpgames16.PlayerScripts.Melee_Local', Line 7  -  Studio - Melee_Local:7
  15:49:44.428  Stack End  -  Studio

Forgot to add locals to the variables. Ensure you parent the track too.

1 Like

This works, but why? What’s the difference? Is the variable name the issue like @benpinpop mentioned?

1 Like

So I tested stuff, and here’s some code that I seem to get working.

local Char = game.Players.LocalPlayer.Character
print(Char.Name)
local Track = Instance.new("Animation")
Track.AnimationId = "rbxassetid://6929015157"
Track.Parent = workspace	
local Anim = Char:WaitForChild("Humanoid").Animator:LoadAnimation(Track)
Anim:Play()
1 Like

How do I parent the track, and is that necessary if its being loaded into the humanoid directly?

1 Like

Just tested it in studio, it’s not required, works fine with the code above, even without parenting.

I don’t know why, but it keeps making the Character nil, doesn’t make much sense to me.

  15:57:29.584  Players.hpgames16.PlayerScripts.Melee_Local:6: attempt to index nil with 'Name'  -  Client - Melee_Local:6
  15:57:29.584  Stack Begin  -  Studio
  15:57:29.584  Script 'Players.hpgames16.PlayerScripts.Melee_Local', Line 6  -  Studio - Melee_Local:6
  15:57:29.585  Stack End  -  Studio

Here’s the error that I’m having, it won’t even print the character or play the animation.

-- Variables --
local UserInputService = game:GetService("UserInputService")
local RemoteEvent = game.ReplicatedStorage.HardWork
local Player = game.Players.LocalPlayer
local Char = game.Players.LocalPlayer.Character
print(Char.Name)

-- Function --
UserInputService.InputBegan:connect(function(input)

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		RemoteEvent:FireServer()
		print ("Remote successfully fired.")
	end

	local Track = Instance.new("Animation")
	Track.AnimationId = "rbxassetid://6929015157"
	local Anim = Char:WaitForChild("Humanoid").Animator:LoadAnimation(Track)
	Anim:Play()

end)```
1 Like

I made the variable name of “Player” to “play” instead, and it’s back to working.

1 Like

I’m gonna ask for the sake of asking, is it a local script? Even though it is.

1 Like

For some reason putting a wait helped, even though wait for child gave me infinite yield earlier.

Yes, it is in a local script, and adding a one second wait fixed it.

1 Like

The code is running too fast so it doesn’t know what the RemoteEvent is

local UserInputService = game:GetService("UserInputService")
local RemoteEvent = game.ReplicatedStorage:WaitForChild("Hardwork")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

-- Function --
UserInputService.InputBegan:connect(function(input)

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		RemoteEvent:FireServer()
		print ("Remote successfully fired.")
	end

	local Track = Instance.new("Animation")
	Track.AnimationId = "rbxassetid://6929015157"
	local Anim = Char:WaitForChild("Humanoid").Animator:LoadAnimation(Track)
	Anim:Play()
end)
1 Like