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

Why does my output say: "attempt to index upvalue 'LocalPlayer' (a nil value)"?

Asked by 5 years ago

Hi, my script under here make a output: "attempt to index upvalue 'LocalPlayer' (a nil value)"

local PlayerService = game:GetService("Players")
local LocalPlayer = PlayerService.LocalPlayer

Thank you for reading, if you know how to help me. Feel free to tell me. -Toby

0
LocalPlayer is nil because the script is not a LocalScript. The server cannot use LocalPlayer because there is no defined "local player." DeceptiveCaster 3761 — 5y
0
Do you know, how to get a local player? tobiO0310 58 — 5y
0
Use a local script. On the server, the player can be obtained by PlayerAdded. DeceptiveCaster 3761 — 5y
0
;---; Thanks for the help. Now I gotta use a remote event to get the name. :/ tobiO0310 58 — 5y
View all comments (3 more)
0
You don't need a RemoteEvent. You can use the PlayerAdded event. DeceptiveCaster 3761 — 5y
0
I used that, but when I teleport, it teleports everyone in the server. :/ tobiO0310 58 — 5y
0
@tobiO0310 Ignore him. You do not need remotes nor a player added event. Your problem is when to use local scripts and when to use server scripts. My awnser is below and it should solve your problem. All you need to do is understand basic Server/Client. This is understanding on when to use scripts, and when to use local scripts in different scenarios. Additionally, there are places where to create RBLXNogin 187 — 5y

1 answer

Log in to vote
0
Answered by
RBLXNogin 187
5 years ago
Edited 5 years ago

The solution to your problem is very simple: Servers and Clients. Server scripts are meant to control aspects of the game for every player to see. Local scripts do the opposite. LocalScripts control content for the specific player to see. Some functions and objects are only allowed to be accessed by local scripts. The keyword 'LocalPlayer' can only be acessed in a local script, not a server script.

Scripts runs "server sided" (which basically means on the roblox server), which allows scripts to be ran for all players. Local scripts run "on your computer/ for the current player". This uses Client-Sided events and properties such as getting the player with game.Players.LocalPlayer or getting the camera/mouse.

When trying to access LocalPlayer from a normal script (server script), it returns an error letting you know that 'LocalPlayer' is nil, because it has an undefined value. Error: "attempt to index upvalue 'LocalPlayer' (a nil value)"

The solution is instead of placing this code inside a script, you insert it inside a Local script.

Look at the "PromptPassPurchase" script to identify what a local script looks like

One more thing, since LocalScripts run on the client you are not allowed to place them in server areas. For now, just remember to put them in "Player" areas so that they run to the client. Place your script inside StarterPlayer, StarterGui,PlayerScripts,etc.

Here is the solution to your problem. If this helps, please check awnser and I would see you later!

Ad

Answer this question