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

Attempt To Index Nil With 'Character'?

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)

2 comments

0

And you're positive its a localscript?

0

Yes, I'm positive

2 answers

✓ Accepted answer

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)

11 comments

0

I meant starterplayerscripts.

0

Make sure to accept this answer if it helped you!

0

Make sure its a localscript not a normal script

0

This works because I tested it out!

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)

0

Then you're doing something wrong.

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

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

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

0

I guess I'll accept your answer then, since it helped me get the HRP

0

if you need more help, dm me

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

2 comments

0

Actually this is in a LocalScript

0

then your local script is in a wrong service, place in in StarterPlayer/StarterCharacterScripts or StartcerPlayerScripts depending on what you want to do

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