How to check something forever

So I am trying to make a Player list.
So I made a local script in starterGui. It does not work and only shows me one player in the game.

Here is my code:

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local PlayerList = Players:GetChildren()
local y = 0


while true do
	for i,v in pairs(PlayerList) do
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = Players.LocalPlayer.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50
		newTem:Destroy()
	end
end

Since this only happens once I made a while loop but it just shows me the same player lots of times and it really lags.

Hello , try adding a " wait() " in the while loop because the game usually crashes when you are not adding wait . ( I’m sorry I’m a beginner )

1 Like

It is not updating the PlayerList for when new players join

while true do
	for i,v in pairs(game.Players:GetPlayers()) do
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = Players.LocalPlayer.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50
		newTem:Destroy()
	end
end

Also I don’t see a wait here, wouldn’t that cause the loop to crash your studio?

Not sure if this the way to go about it, but you firstly your setting the text as the LocalPlayer so its always going to be your name, try putting it v.Name. I would also recommend changing the Playerlist to :GetPlayers() instead of :GetChildren().

I added a wait and it now just doesn’t show anything.

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local PlayerList = Players:GetChildren()
local y = 0


while true do
	for i,v in pairs(game.Players:GetPlayers()) do
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = Players.LocalPlayer.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50
		newTem:Destroy()
		wait(5)
	end
end

I think the wait should be placed outside the in pairs loop

I did not work I moved it outside the while loop

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")
local PlayerList = Players:GetChildren()
local y = 0


while true do
	for i,v in pairs(game.Players:GetChildren()) do
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = Players.LocalPlayer.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50
		newTem:Destroy()
		
	end
	wait(1)
end

Maybe try changing

newTem.Text = Players.LocalPlayer.Name

To

newTem.Text = v.Name

Which could solve your first issue of the same palyer appearing. And I think you have to reset y after the in pairs ends since it’ll eventually have a y value that’ll go past the screen

Edit: And if you’re trying to make a list of players that appear in frames, then yea I would recommend what @http_shawn mentioned since doing this seems a bit excessive

the reason your not seeing them is because your not updating the player list, so its only showing the initial players in game. By updating it, you can see whenever a player joins / leaves. Also please consider using UIlistlayout to easily remove and add gui objects and position them correctly. And maybe instead of updating it consistently only update on player added or player removed.

1 Like

Add the wait at the start of the while loop because it’s doing the for loop before it waits

It does not work.

local tem = game.ReplicatedStorage:WaitForChild("Template")
local frame = script.Parent:WaitForChild("Frame")
local Players = game:GetService("Players")

local y = 0


while true do
	wait(1)
	local PlayerList = Players:GetChildren()
	for i,v in pairs(PlayerList) do
		local newTem = tem:Clone()
		newTem.Parent = frame
		newTem.Text = Players.LocalPlayer.Name
		newTem.Position = UDim2.new(0,0,0,y)
		y += 50
		newTem:Destroy()
	end
	wait(10)
end

whats not working? also you can delete the wait(1) after the while true do

It is not showing the template.

How can I do that with my script?

What I would personally do is create a Add Function like this and use tables to store the current players:

local PlayersTable = {}

local function Add()
   for Index, Value in ipairs(game:GetService("Players"):GetPlayers()) do
      if PlayersTable[Value.Name] ~= nil then return end -- Player is already in the table
      PlayersTable[Value.Name] = true -- Not sure if you need .Name

      local Template = [AreaOfTemplate]:Clone() -- Wherever your template is 
      Template.Name = Value.Name -- Again not sure if you need .Name
      Template.Parent = -- The frame | I would use a UIListLayout you don't need
                        -- to position it correctly
   end
end

Now that we have a Add function we will create a Remove Function whenever the player leaves:

local function Remove(PlayerLeaving)
   if PlayerLeaving then
       -- Destroy the playerlist frame
       PlayerTable[PlayerLeaving.Name] = nil -- Remove from table | Anyway you prefer to remove them from table
   end
end

Thats it for the Removing function now for the PlayerAdded and PlayerRemoving Events

local Players = game:GetService("Players") 

Players.PlayerAdded:Connect(function()
    Add()
end)

Players.PlayerRemoving:Connect(function(player)
    Remove(player)
end)

Add() -- Do this just incase 

Hopefully this helps you out!

try this

if that doesn’t work then i think its something to do with your positioning. Use Uilistlayout

and also use player added and player removing like mentioned in @TheDCraft 's post

1 Like

It did not work. The first script showed the player template.

It did not work.

local PlayersTable = {}

local function Add()
	for Index, Value in ipairs(game:GetService("Players"):GetPlayers()) do
		if PlayersTable[Value.Name] ~= nil then return end -- Player is already in the table
		PlayersTable[Value.Name] = true -- Not sure if you need .Name

		local Template = [game.ReplicatedStorage:WaitForChild("Template")]:Clone() -- Wherever your template is 
		Template.Name = Value.Name -- Again not sure if you need .Name
		Template.Parent = script.Parent:WaitForChild("Frame") -- The frame | I would use a UIListLayout you don't need
			-- to position it correctly
	end
end

local function Remove(PlayerLeaving)
	if PlayerLeaving then
		-- Destroy the playerlist frame
		PlayerTable[PlayerLeaving.Name] = nil -- Remove from table | Anyway you prefer to remove them from table
	end
end

local Players = game:GetService("Players") 

Players.PlayerAdded:Connect(function()
	Add()
end)

Players.PlayerRemoving:Connect(function(player)
	Remove(player)
end)

--Add() -- Do this just incase

please send screenshots so we can better understand.