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

Playerlist not functioning properly?

Asked by 8 years ago
Edited 8 years ago
01local frame = script.Parent
02local labels = {}
03 
04for i = 1, game.Players.MaxPlayers do
05    labels[i] = frame:WaitForChild("Player"..i)
06end
07 
08local players = game.Players:GetPlayers()
09 
10for number = 1, #labels do
11    labels[number].Text = tostring(players[number] or "")
12end

08:39:02.906 - Infinite yield possible on 'Players.Player1.PlayerGui.PlayerListUI.PlayerListFrame:WaitForChild("Player11")' 08:39:02.908 - Script 'Players.Player1.PlayerGui.PlayerListUI.PlayerListFrame.LocalScript', Line 7 08:39:02.909 - Stack End I have 10 TextLabels inside the frame, and I have it set so the MaxPlayers is 10 per server. It works when I join the actual game, but it doesn't work in studio, and I have the error that is printed in the output above.

1 answer

Log in to vote
2
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

You don't need to be checking how many players there are, you can just iterate through the labels and set the text of that label to the name of that player (or blank if there are no more players.)

1local frame = script.Parent
2local labels = {}
3for i = 1, game.Players.MaxPlayers do
4    labels[i] = frame:WaitForChild("Player"..i)
5end
6local players=game.Players:GetPlayers()
7for number = 1, #labels do
8    labels[number].Text = tostring(players[number] or "")
9end

edit: If you want it to update whenever a player joins/leaves, connect it to PlayerAdded and PlayerRemoving:

01local frame = script.Parent
02local labels = {}
03for i = 1, game.Players.MaxPlayers do
04    labels[i] = frame:WaitForChild("Player"..i)
05end
06function update()
07    local players=game.Players:GetPlayers()
08    for number = 1, #labels do
09        labels[number].Text = tostring(players[number] or "")
10    end
11end
12game.Players.PlayerAdded:connect(update)
13game.Players.PlayerRemoving:connect(update)
14update()
0
Ok, it does work, but a slight problem. If I am on the server it has my name, but if my friend joins, their name doesn't add onto the list for me? NinjoOnline 1146 — 8y
0
I'm not sure if it's just studio, but now when I test it, it works for player1, but nothing appears on player2s screen? Does it only work in actual game servers and not the studio servers? NinjoOnline 1146 — 8y
0
And another problem, it doesn't work in online mode NinjoOnline 1146 — 8y
0
It only seems to work when a second player joins :/ NinjoOnline 1146 — 8y
View all comments (4 more)
0
added update() to line 14 1waffle1 2908 — 8y
0
Works! Thank you!! NinjoOnline 1146 — 8y
0
One last question, if I do players[number] that returns the player who joined, but nil for the other 9 spots. I'm trying to make it so if a player joins witha certain name they get different colored text. I edited my question with what I have but it doesn't work :/ NinjoOnline 1146 — 8y
0
Wait nvm, think I got it NinjoOnline 1146 — 8y
Ad

Answer this question