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

Trying to change a line of code but it sends me an error in-game, Help?

I tried alot of try it including :FindFirstChild and :FindFirstChildOfClass but anything doesn't work for it and Parents won't even work for it. It says game.Players.LocalPlayer.Character is a nil value but Player is present in the game. I want to get rid of this error! Help? Every samples of line of code I use that still won't works:

local player = game.Players.LocalPlayer.Character
local humanoid = player.Humanoid --not working
local player = game.Players.LocalPlayer.Character
local humanoid = player:FindFirstChild("Humanoid") --also not working
local player = game.Players.LocalPlayer.Character
local humanoid = player:FindFirstChildOfClass("Humanoid") --the worst of all that does not working

1 comment

2

Please post the script so we can further help you.

1 answer

✓ Accepted answer

###You are correct, the Player is present, but is the Character? No. You should wait for the character to be loaded. It is not good to assume it has loaded, neither is it good to assume it hasn't. Using .Parents to access a player is bad practice, as well as time consuming.

wait() -- it's good practice to add a little wait() at the beginning, makes sure things are loaded
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

###If player.Character returns nil, it will wait for the character to be loaded instead.

2 comments

0

Thanks! But I have to test this in client to make sure this answer is correct.

0

It's a huge help thought! Since the humanoid didn't work, I used or's so when one didn't work, it has a replacement!

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