Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempted to Index nil With Character?

I am trying to make an animation when you pick up stuff but it keeps saying:

Workspace.Part.Script:2: attempt to index nil with 'Character'

script.Parent.ClickDetector.MouseClick:Connect(function()
	local ani = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Pickup)
	ani:Play()
end)

6 comments

0

it cant find ani thats why

0

ani is in the click detector function thing but your looking for it in a place it isnt

0

You are trying to interact with something that doesnt exist.

1

Server-sided Scripts aren't localized, obviously, therefore no pointer exists to a linked Client Instance. Fortunately, the MouseClick RBXScriptSignal provides a parameter that refers to the Player Object of the user that clicked, so you can simply utilize that.

1

CJ, "ani" is short for "Animation". It's a variable used to reference the AnimationTrack Object that's returned by the :LoadAnimation() method of Humanoid.

1

If you don't need to reference the Object anywhere else, Error_404, then declaring a variable is redundant. You can directly call the :Play() method onto :LoadAnimation() due to it's Object-return.

1 answer

✓ Accepted answer

Local player cannot be used in Server Sided script. However, you can utilize the first parameter of a click which is the player who clicked it. Try this out:

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	local ani = plr.Character.Humanoid:LoadAnimation(script.Parent.Pickup)
	ani:Play()
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.