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

Players.2_MMZ.BackPack.SwordScript:124 attempted to index nil with character?

I'm trying to make a sword that plays an animation when you click. But I get an error. (Stated above in the title) It's a serverscript inside of the gear.

local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)

animation:Play()

Any answer that actually works is fine.

10 comments

0

Maybe try local script?

1

Then it won't appear on other people's screens

1

ok what 3k views and no answers

0

ikr

0

It's probably just bots.

0

maybe try: local animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animation) , idk i havent try it

1

@The_Saver31 That leads to the starterpack.

1

I believe it is a Server/Normal script. And the thing to note in the server/normal scripts, is that there is no Local Player, it is indexed as nil (as server doesn't work for any particular player but for all players). And in order to fix it, you can just get the Parent of your tool, because if you activate/equip the tool if gets moved to the player's Character., to get the player's character.

0

Finally someone who gets it ^ ;)

0

Don't answer the question in the comments.

1 answer

✓ Accepted answer

You should run this animation on a localscript. Player characters have an animator inside of their humanoids. This is for the purpose of client-server replication of animations. Meaning, you can play an animation on a localscript on the client and it will still be able to be seen by other players.

Furthermore, if you are going to run this in a localscript, I suggest you yield until the player's character exists, because it takes some time for the character to load.

-- In LocalScript
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player:WaitForChild("Character")
local Humanoid = Character:WaitForChild("Humanoid")

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://..."
local AnimTrack = Humanoid:LoadAnimation(Animation)

AnimTrack:Play()

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