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

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

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

1 comment

0

depending on where this is placed , the player's Character may or may not exist when that is ran

2 answers

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")
local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

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