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

LocalPlayer Error: Tried To Index Field a Nill Value?

Asked by 6 years ago

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.

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. User#5423 17 — 6y
0
Without additional information I cannot help. User#5423 17 — 6y
0
It's a script in the workspace. FlippinAwesomeCrew 62 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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!

0
Thanks, I will use MoveTo() but by the way I did try CFrame in another script and it worked. FlippinAwesomeCrew 62 — 6y
Ad

Answer this question