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

Help, this simple script does not work?

Asked by 9 years ago

This simple script does not work. It is in a text label and it is a local script.

local label = game.StarterGui.NumPlayerGui.TextLabel -- if I don't access it like this then the error will be tried to index global script a nil value

while true do
    label.Text = "Players In Server: ".. game.Players.NumPlayers
    wait(1)
end

In the properties the text changes but in the game the text does not change. It is weird.

2 answers

Log in to vote
0
Answered by 9 years ago

Because NumPlayers is not a value of players, and two, you dont need that long variable. plus, "StarterGui" is a place in bject unto which you put the gui. Its complicated. if you understand hiarchy and such, here is how it works: the objects in the startergui and starterpack containers clone into a new players "Backpack" and "PlayerGui". so the way you have it, it wont change anyway, but if it did, it would change the one in the place nobody sees until they spawn. What you need is the following script:

local label = game.StarterGui.NumPlayerGui.TextLabel--this is you main problem, fixed in the next script
while true do
    label.Text = "Players In Server: ".. game.Players.NumPlayers--"NumPlayers is nonexistent
    wait(1)
end

heres the one you are looking for, and you put this in the label in the screeengui, in the startergui and actually, it should be whenever a new player enters, so we make it a function, not a loop.

local label = script.Parent
local NumPlayers = 0
game.Players.PlayerAdded:connect(function()
for i, v do in pairs(game.Players:GetChildren()) do
NumPlayers = Numplayers+1
end
    label.Text = "Players In Server: " ..NumPlayers
NumPlayers = 0
end)

The reason why it was made a function was because it will work the best, the for loop because it gives an accurate number, and we changed it back to 0 to reset. its all on the iki for a basic outline, as long as you can put two and two together.

0
I just looked a t the script, i didnt realize Players.NumPlayers was a actual value. But this script works just as well and to be honest, is probably more efficient, to reduce lag, and you can take out the for loop, and replace the variable of "NumPlayers = 0" to "NumPlayers = game.Players.NumPlayers" rollercoaster57 65 — 9y
0
 PlayerAdded events don't work well in local scripts for they only fire for their specific client! Perci1 4988 — 9y
0
But it still works non the less, and they will fire at the same time, and it doesnt have to be a local script. rollercoaster57 65 — 9y
0
Aye, it may work as a server script, but it would be most efficient as a LocalScript considering that it's inside the Player. Perci1 4988 — 9y
View all comments (2 more)
0
Yes, and here is the thing, PlayerAdded:connect() will fire at the same time, this will prevent much lag throughout the client, instead of a loop. rollercoaster57 65 — 9y
0
My point is that PlayerAdded events don't work as expected in LocalScripts; http://wiki.roblox.com/index.php?title=PlayerAdded Perci1 4988 — 9y
Ad
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

This mistake is made a lot. StarterGui is in no way connected to what a player is seeing.

StarterGui is simply a holder that contains useless GUIs that will later be cloned and distributed to individual PlayerGuis.

PlayerGui is located inside each Player, and contains GUI objects that will be visible on that Player's screen.

Each time a Player respawns, PlayerGui will be refreshed. All the GUIs currently in it will be destroyed, and replaced with whatever is currently in StarterGui.

It works similarly to StarterPack; StarterPack is not what is in your Backpack, but contains gear objects that will later be put into your Backpack.

Just put the script inside the TextLabel, then access it through script.Parent.

local label = script.Parent

while true do
    label.Text = "Players In Server: ".. game.Players.NumPlayers
    wait(1)
end

0
script.Parent always gives me an error: Attempt to index global 'script', a nil value. That error I get whenever I use script.Parent or anything with script raystriker6707 30 — 9y
0
Is there a setting so I can change this? raystriker6707 30 — 9y
0
That really makes no sense and I have no idea why that would happen. It's most likely that when that happened you misspelled 'script'. Perci1 4988 — 9y

Answer this question