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

How do I get Humanoid In to a variable LocalScript in StarterPack in Tool?

Asked by 4 years ago
Edited 4 years ago

Hello I was making my script and I had to make a variable for humanoid. This is my script

local humanoid = game.Players.LocalPlayer.Character.Humanoid

I get errors like this

Players.applebugr21.Backpack.L.1Sword.AnimationLocalScript:2: attempt to index field 'Character' (a nil value)

when I run the scrip. What is wrong with is code? Help me

0
depending on where this is placed , the player's Character may or may not exist when that is ran theking48989987 2147 — 4y

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Depending on some scenarios, the Character Rig may not load in before the program makes an attempt to allocate it, instead, we tell our program to yield until it does.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")
Ad
Log in to vote
-1
Answered by 4 years ago
local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

Answer this question