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

LocalPlayer Error: Tried To Index Field a Nill Value?

So I'm making a game (duh) and I want it so if your on a team, then you will be spit onto one of four spawn points.
Here's the script:

KnightSpawnPoint = math.random(1,4)
if game.Players.LocalPlayer.TeamColor == "Really Red" and KnightSpawnPoint == 1 then
	game.Players.LocalPlayer.Character.Head.CFrame = CFrame.new(447, 105.225, 299.22)
elseif game.Players.LocalPlayer.TeamColor == "Really Red" and KnightSpawnPoint == 2 then
	game.Players.LocalPlayer.Character.Head.CFrame = CFrame.new(312.7, 105.225, 303.22)
elseif game.Players.LocalPlayer.TeamColor == "Really Red" and KnightSpawnPoint == 3 then
	game.Players.LocalPlayer.Character.Head.CFrame = CFrame.new(315.7, 108.713, 432.77)
elseif game.Players.LocalPlayer.TeamColor == "Really Red" and KnightSpawnPoint == 4 then
	game.Players.LocalPlayer.Character.Head.CFrame = CFrame.new(446.775, 108.713, 432.77)
end

Now when I do this I get this error: 13:56:20.867 - Workspace.Script:2: attempt to index field 'LocalPlayer' (a nil value) Can you please help me with this problem, much appreciated.

3 comments

0

A server script has no concept of a local player since it runs for all players. You will need to get the player to change their CFrame. Local script run in one place the client side so it knows who it is running for.

0

Without additional information I cannot help.

0

It's a script in the workspace.

1 answer

Please note I did not test some things I said, just saying what I think and know. So sorry if I end up saying something inaccurate.

As what kingdom5 said, server scripts can’t use LocalPlayer. I think it’s possible to move the localplayer’s position in a localscript though, so just change the script into a localscript to make things easier for you.

I also prefer you use MoveTo() instead of changing the CFrame of the head as I think changing the CFrame of the head will kill the player, while MoveTo() changes the position of the whole model.

Hoped this helped!

1 comment

0

Thanks, I will use MoveTo() but by the way I did try CFrame in another script and it worked.

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