Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by
2_MMZ 1059 Moderation Voter
3 years ago

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.

0
Maybe try local script? The_Saver31 260 — 3y
1
Then it won't appear on other people's screens 2_MMZ 1059 — 3y
1
ok what 3k views and no answers 2_MMZ 1059 — 3y
0
ikr KixWater 126 — 3y
View all comments (6 more)
0
It's probably just bots. KixWater 126 — 3y
0
maybe try: local animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animation) , idk i havent try it The_Saver31 260 — 3y
1
@The_Saver31 That leads to the starterpack. 2_MMZ 1059 — 3y
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. BestCreativeBoy 1395 — 3y
0
Finally someone who gets it ^ ;) Ziffixture 6913 — 3y
0
Don't answer the question in the comments. radiant_Light203 1166 — 3y

1 answer

Log in to vote
1
Answered by
tantec 305 Moderation Voter
3 years ago

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()
Ad

Answer this question