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

Attempt To Index Nil With 'Character'?

Asked by 3 years ago

I'm trying to get the Character from the Player in a LocalScript so I can get the HumanoidRootPart to teleport them, but every time I try to get the HRP, it just errors: attempt to index nil with 'character' (on line 2)

The code:

local Player = game.Players.LocalPlayer
local HRP = Player.Character.HumanoidRootPart
print(HRP.Position)
0
And you're positive its a localscript? xd_Lxcifer -3 — 3y
0
Yes, I'm positive SuperSM1 67 — 3y

2 answers

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

This script should go in a localscript in StarterPlayer< StarterPlayerScripts

Code:

local Player = game:WaitForChild("Players").LocalPlayer
local Character = Player.CharacterAdded:Connect(function(char)
local HRP = Player.Character:WaitForChild("HumanoidRootPart")
if Player.Character then
    print(HRP.Position)
    end
end)
0
I meant starterplayerscripts. JesseSong 3916 — 3y
0
Make sure to accept this answer if it helped you! JesseSong 3916 — 3y
0
Make sure its a localscript not a normal script JesseSong 3916 — 3y
0
This works because I tested it out! JesseSong 3916 — 3y
View all comments (7 more)
0
It sadly doesn't work for me, I run it and it says 'attempt to index nil with 'CharacterAdded'' (I made sure it's a LocalScript and it's in StarterPlayerScripts) SuperSM1 67 — 3y
0
Then you're doing something wrong. JesseSong 3916 — 3y
0
Wait nevermind, it works if I play it, but not if I run it, thanks! I'll test it out a few more times to see if it works all the time and if it does I'll accept your answer SuperSM1 67 — 3y
0
Well... I tried to do the thing I want, which is teleporting them, but now there's no errors no nothing, it just doesn't work SuperSM1 67 — 3y
0
game.Workspace:WaitForChild("InsideContent") local TP = game.Workspace.InsideContent.TPWallLeft TP.Touched:Connect(function(TPActive) local Player = game:WaitForChild("Players").LocalPlayer local Character = Player.CharacterAdded:Connect(function(char) local HRP = Player.Character:WaitForChild("HumanoidRootPart") if Player.Character then local Seat = Vector3.new(game.Workspace.Transporta SuperSM1 67 — 3y
0
I guess I'll accept your answer then, since it helped me get the HRP SuperSM1 67 — 3y
0
if you need more help, dm me JesseSong 3916 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

it's very straight actually, "attempt to index nil with 'character'" so there player = nil

why? Players.LocalPlayer can be used only inside a local script and CAN NOT be used inside a server script and so it will return nil, this is probably your problem

0
Actually this is in a LocalScript SuperSM1 67 — 3y
0
then your local script is in a wrong service, place in in StarterPlayer/StarterCharacterScripts or StartcerPlayerScripts depending on what you want to do zeyadtota3 7 — 3y

Answer this question