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

Why LocalPlayer doesn't work in game but it works in studio?

Asked by 9 years ago

I have tried many ways like using local script but still doesn't work so I try to use normal script again. For the local script the LocalPlayer works but my other script doesn't work, it stops. Maybe it's because I put all of the script together. I'm confused about the local script and LocalPlayer. The script is for minigame that I am making. So here's my script :

01--start
02game.Players.ChildAdded:wait()
03--variables
04Map1 = game.ServerStorage.Maps.Arena1
05NotificationGui1 = game.Workspace.StatusBar.Gui1.TextBox.Text
06NotificationGui2 = game.Workspace.StatusBar.Gui1.TextBox.Text
07local Player1 = game.Players.LocalPlayer
08Winner = "None"
09WinnerFound = false
10enabled = true
11Reciver = game.Workspace.Spawn.Spawn1
12 
13--Reciver Vars
14Spawn1o = 0
15Spawn2o = 0
View all 91 lines...
0
because local player is local hos, thus your computer is not some else's local host, if you want the code to work either place it in starter gui, starter tools, playerscripts, scottmike0 40 — 9y
0
on a side note the only real difference from using local script vs script is organization of code handling, and knowing that you placed the right code in the right place,because calling local player simply will be null unless it is in one of the catagories that i have previously mentioned, also it allows the user not to see code through a 3rd party application, in terms of difference vs local scri scottmike0 40 — 9y
0
Code in a ServerScript runs strictly on the server you are connected to, where as code in a LocalScript will run strictly on the client/player's computer. That is the difference between the two. BlackJPI 2658 — 9y
0
@blackJPI I explained that more simple scottmike0 40 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

If it's in a LocalScript:

You're waiting for a Player to join. Unfortunately, when you join that event will not fire for LocalScripts listening to it. That in turn means that unless another player joins the game, the LocalScript is never going to get past line 1.


If it's in a Script

You can't use Players.LocalPlayer in a Script, because it simply doesn't know anything about it on the server. You can get the first Player to join if you replace your first line with

1local Player1 = game.Players.PlayerAdded:wait()

Which will assign Player1 to the first Player to join the server. Be aware that this may or may not play nice with how you expect your game to work, and how or where you do stuff is down to you.

Ad
Log in to vote
0
Answered by 9 years ago

In studio, it makes everything run through the server (studio wise). In-game, you must use a local script when connecting to a local player and do not use localscripts for anything other than the client's children. This script would not work with multiple users if you're attempting to use it in a local script and same with a server script.

Local Reason: It'd run on all clients and be different for everyone causing your game to look very glitchy and buggy because instead of running one time, it'll be running several times through several clients.

Server Reason: You cannot connect to a local player through server scripts, if you're attempting to grab players I recommend you use

1for i,v in pairs(game.Players:GetChildren()) do
2print(v.Name) -- v represents the player.
3end

Answer this question