Alright here is the code:
local frame = script.Parent local num = frame.Players local template = frame.Template for i, v in pairs(game.Players:GetPlayers()) do if v.Character then player1 = v end end function Joining(player) num.Value = num.Value + 1 local slotnum = num.Value local slot = template:Clone() slot.Parent = frame slot.Name = "Slot"..slotnum slot.Position = UDim2.new(0,0,0,55*num.Value) slot.Text = player1.Name slot.Visible = true end function Leaving(player) for i,label in pairs(frame:GetChildren()) do if label then if string.find(label.Name,"Slot") then if label.Text == player.Name then label:Destroy() num.Value = num.Value - 1 else end end end end end script.Parent.Parent.Submit.MouseButton1Down:connect(Joining) game.Players.PlayerRemoving:connect(Leaving)
And I am getting this error: Workspace.Part.SurfaceGui.Hosts.ListScript:18: attempt to index global 'player1' (a nil value)
I just want it to print the player's name onto the surfacegui.
This code doesn't make any sense.
for
loop at the beginning accomplishes nothingJoining
is attached to when you click a button rather than when anyone joinsIt appears that you want to make a list of players, each with their own GUI frame. In that case...
num
-- at best you are storing a list that you can guess the number fromBecause of the last detail, we need to maintain a full list of GUIs paired with players. But it's not too complicated.
The simplest solution is to be able to "update" based on the player list:
function update(playerList) frame:ClearAllChildren() for i, player in pairs(playerList) do local slot = template:Clone() slot.Parent = frame slot.Position = UDim2.new(0, 0, i * 55) slot.Text = player.Name slot.Visible = true end end local players = {} -- List of players currently in the game game.Players.ChildAdded:connect(player) -- For stupid reasons, you cannot use PlayerAdded from a LocalScript if player:IsA("Player") then table.insert(players, player) update(players) end end game.Players.ChildRemoving:connect(player) -- For stupid reasons, you cannot use PlayerRemoving from a LocalScript if player:IsA("Player") then for i = 1, #players do if players[i] == player then table.remove(players, i) end end update(players) end end
Note that if you don't mind the list re-arranging itself (though this is unlikely anyway), you can eliminate even more:
-- just use :GetPlayers() instead of having a parameter? function update() frame:ClearAllChildren() for i, player in pairs( game.Players:GetPlayers() ) do local slot = template:Clone() slot.Parent = frame slot.Position = UDim2.new(0, 0, i * 55) slot.Text = player.Name slot.Visible = true end end game.Players.ChildAdded:connect(update) game.Players.ChildRemoving:connect(update)
Instead of using 'Player1' because the number value next to the variable will confuse the script. Remove the 1 then retest it.
Just add this to the top of the script to make sure that there are players in the game to work with that have an existant character:
repeat wait() until game.Players.NumPlayer>=1 and game.Players:GetChildren()[1].Character
All this does is wait until the player has a character that the game is able to work with.
So the script all together should look like this:
repeat wait() until game.Players.NumPlayer>=1 and game.Players:GetChildren()[1].Character local frame = script.Parent local num = frame.Players local template = frame.Template for i, v in pairs(game.Players:GetPlayers()) do if v.Character then player1 = v end end function Joining(player) num.Value = num.Value + 1 local slotnum = num.Value local slot = template:Clone() slot.Parent = frame slot.Name = "Slot"..slotnum slot.Position = UDim2.new(0,0,0,55*num.Value) slot.Text = player1.Name slot.Visible = true end function Leaving(player) for i,label in pairs(frame:GetChildren()) do if label then if string.find(label.Name,"Slot") then if label.Text == player.Name then label:Destroy() num.Value = num.Value - 1 else end end end end end script.Parent.Parent.Submit.MouseButton1Down:connect(Joining) game.Players.PlayerRemoving:connect(Leaving)
Anyways, hope this helped. If you have any further questions/problems, please leave a comment below :P