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

Why game.Players.LocalPlayer isn't working on online?

Asked by 7 years ago

while wait() do local player = game.Players.LocalPlayer script.Parent.Text = "Kills:" ..player:WaitForChild("leaderstats"):FindFirstChild("Kills").Value end

0
What do you think the server thinks you want when you do LocalPlayer? It's not that smart, y'know. hiimgoodpack 2009 — 7y
0
This script is in Textlabel and Textlabel is in a Gui and if he has a gui,i want to find he on game.Players. FrezeTagger 75 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

When using GUIs I recommend referencing the player directly, whether it's via game.Players.ViktorCorvinus or script.Parent.Parent.Parent.Parent,

Sincerely Yours, -ViktorCorvinus

0
There is nothing wrong with using game.Players.LocalPlayer chess123mate 5873 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

LocalPlayer is only valid when you're in a LocalScript, and LocalScripts only run in certain on the client (not the server), and only in certain locations (ex they don't run in ReplicatedStorage, most places in the Workspace, etc). This scripts works offline for you because when you test things in "Play" mode in Studio, your computer acts both as the server and the client. If you use "Test" mode (with a server and a Player), it will act the same as in online mode.

Your script is attempting to keep a TextLabel up to date Kills.Value. Some notes:

  • You only need to assign 'player' once. LocalPlayer isn't going to change.
  • There's almost never a reason to call :FindFirstChild if you aren't going to check if the value is nil first
  • When they do what you want, events are better, as they can get rid of the loop and therefore use less processing time. You wanted to update the TextLabel "when kills changes", so it makes sense that we can use the Changed event on the 'kills' object:
local player = game.Players.LocalPlayer
local kills = player:WaitForChild("leaderstats"):WaitForChild("Kills")
kills.Changed:Connect(function()
    script.Parent.Text = "Kills: " .. kills
end)

Answer this question