Repeating textlabels

I made a local script which makes new instances in a scrolling frame. However it repeats as you could see in the screenshot below.

I want it to display both our names as you can see two users, but it repeats the one.

I tried to do a if loop which checks if the textlabel exists, but perhaps I am applying the wrong logic since it still repeats.

Code in question

local Players = game:GetService("Players")
local frame = script.Parent

local function create_player_gui(userId)
	local name = Instance.new("TextLabel")
	name.TextSize = 24
	name.TextXAlignment = Enum.TextXAlignment.Left
	name.Size = UDim2.new(1, 0, 0, name.TextSize)
	name.Parent = frame
	for _, player in pairs(Players:GetPlayers()) do
		local userId = player.UserId
	local username = Players:GetNameFromUserIdAsync(userId)

		name.Text = username
		end
	end

for _, player in pairs(Players:GetPlayers()) do
	create_player_gui(player.UserId)
end

Try this out?

local Players = game:GetService("Players")
local frame = script.Parent

local function create_player_gui(plr)
	if frame:FindFirstChild(plr.Name) then return end
	print("Making Gui for "..plr.Name)
	local name = Instance.new("TextLabel")
	name.TextSize = 24
	name.TextXAlignment = Enum.TextXAlignment.Left
	name.Size = UDim2.new(1, 0, 0, name.TextSize)
	name.Text = plr.Name
	name.Name = plr.Name
	name.Parent = frame
end

for _, player in pairs(Players:GetPlayers()) do
	create_player_gui(player)
end


It works. But how do I continuously make it loop, whoevers the first, doesnt have an updated list when a playerisadded

Hmm, maybe include a PlayerAdded event for any other players that join?

local Players = game:GetService("Players")
local frame = script.Parent

local function create_player_gui(plr)
	if frame:FindFirstChild(plr.Name) then return end
	print("Making Gui for "..plr.Name)
	local name = Instance.new("TextLabel")
	name.TextSize = 24
	name.TextXAlignment = Enum.TextXAlignment.Left
	name.Size = UDim2.new(1, 0, 0, name.TextSize)
	name.Text = plr.Name
	name.Name = plr.Name
	name.Parent = frame
end

Players.PlayerAdded:Connect(function(plr)
	create_player_gui(plr)
end)

for _, player in pairs(Players:GetPlayers()) do
	create_player_gui(player)
end
1 Like

Try implementing this code to your code:
(If it’s make sense)

1 Like

Appreciate that will check it out

1 Like