I need help with a script

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)

Any help is appreciated

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

Its a server script as it did not work with local script

Server scripts CANNOT be used under GUI because it is supposed to focus on client side. Always use Localscript in UIs.

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

untested

The frame already exists tho? Im gonna try using a remote event

Your labels were not ready to be used yet. I took the time to look at and answer your question. Just to have you do us like that …

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.