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
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
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