Hello,
Why is it not creating a label but printing the thing?
script:
-- server script in a frame, frame is parented into a screengui in startergui
local Players = game:GetService("Players")
local frame = script.Parent
local function updatePlayerList()
--for _, label in ipairs(frame:WaitForChild("PlrList"):GetChildren()) do
-- label:Destroy()
-- end
local playerNames = {}
for _, player in ipairs(Players:GetPlayers()) do
table.insert(playerNames, player.Name)
end
for _, playerName in ipairs(playerNames) do
local label = Instance.new("TextLabel")
label.Parent = frame:WaitForChild("PlrList")
label.Text = playerName
label.TextScaled = true
label.Visible = true
label.BackgroundColor3 = Color3.new(0.192157, 0.717647, 1)
print("label created for: " .. playerName) -- printing this
end
end
-- god help me my brain abnout do to boom boom
updatePlayerList()
Players.PlayerAdded:Connect(updatePlayerList)
Players.PlayerRemoving:Connect(updatePlayerList)
(if this is a LocalScript)
I’ve had a bit of trouble using Instance.new() and setting a Parent within LocalScripts - There seems to be an issue with it client-side. Setting the Parent without Instance.new() seems to work fine, though - try using a pre-made label and cloning it to the GUI.
If that doesn’t work, let me know if there are any error messages in your output.
Also, PlayerAdded and PlayerRemoving don’t like to work client-side - use a RemoteEvent from the server.
server scripts do not have direct access to GUI elements.
If you’re trying to update the player list on the client side, you should use a LocalScript running in a Player’s GUI (like a ScreenGui). You could try to connect the Server with the client by using a RemoteEvent
local Players = game:GetService("Players")
local function updatePlayerListToAllClients()
local playerNames = {}
for _, player in ipairs(Players:GetPlayers()) do
table.insert(playerNames, player.Name)
end
game.ReplicatedStorage.PlayerListUpdate:FireAllClients(playerNames)
end
Players.PlayerAdded:Connect(updatePlayerListToAllClients)
Players.PlayerRemoving:Connect(updatePlayerListToAllClients)
It would seem you’re trying to create labels in the frame before the frame is parented to the ScreenGui. Maybe a bit of restructuring the script would work.
local Players = game:GetService("Players")
local frame = script.Parent
Players.PlayerAdded:Connect(updatePlayerList)
Players.PlayerRemoving:Connect(updatePlayerList)
function updatePlayerList()
local playerNames = {}
for _, player in ipairs(Players:GetPlayers()) do
table.insert(playerNames, player.Name)
end
for _, playerName in ipairs(playerNames) do
local label = Instance.new("TextLabel")
label.Parent = frame:WaitForChild("PlrList")
label.Text = playerName
label.TextScaled = true
label.Visible = true
label.BackgroundColor3 = Color3.new(0.192157, 0.717647, 1)
print("label created for: " .. playerName)
end
end
updatePlayerList()
Gave up, didnt want to make it anymore, sorry i did that, thats not nice of me. I shouldve not even make this forum in the first place. Ur code didnt work anyway so my brain thought: yeah f this. I appreciate that u took ur time to try helping me, but in the end i just gave up. Sorry