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

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

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

2 comments

0

What do you think the server thinks you want when you do LocalPlayer? It's not that smart, y'know.

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.

2 answers

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

1 comment

0

There is nothing wrong with using game.Players.LocalPlayer

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)

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