Help With Script

I’m making a script which updates the player list in my admin panel UI. I have this code:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Main = script.Parent.Parent.Main
local Categories = Main.Categories
local HomeFrame = Categories.Home
local PlayersFrame = Categories.Players

local GetPlayersRemote = ReplicatedStorage:WaitForChild("AdminPortal").GetPlayers
local IsAdminRemote = ReplicatedStorage:WaitForChild("AdminPortal").IsAdmin

-- Launch

UpdatePlayerList()

-- Update Player List

Players.PlayerAdded:Connect(function(Player)
	AddPlayerToList(Player, IsAdminRemote(Player))
end)

Players.PlayerRemoving:Connect(function(Player)
	RemovePlayerFromList(Player)
end)

function UpdatePlayerList()
	for i,v in pairs(PlayersFrame.ScrollingFrame:GetChildren()) do
		if v:IsA("Frame") then
			v:Destroy()
		end
	end

	local plrs = GetPlayersRemote:InvokeServer()

	for i,v in pairs(plrs) do
		AddPlayerToList(v.Player, v.Admin)
	end
end

function AddPlayerToList(Player, Admin)
	local Clone = ReplicatedStorage:WaitForChild("AdminPortal").Player
	Clone.UsernameAndId.Text = Player.Name .. " | " ..  Player.UserId
	Clone.Pfp.Image = Players:GetUserThumbnailAsync(Player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	-- Clone.Rank.Text = rank

	if Admin then Clone.Administrator.Visible = true end
	
	Clone.Name = Player.UserId
	Clone.Parent = PlayersFrame.ScrollingFrame
end

function RemovePlayerFromList(Player)
	for i,v in pairs(PlayersFrame.ScrollingFrame:GetChildren()) do
		if v.Name == Player.UserId then
			v:Destroy()
		end
	end
end

I’m getting an error (attempt to call a nil value) here:

image

I noticed that when I remove the functions and copy & paste the code from the functions, the code decides to work. Any ideas would be greatly appreciated!

Try and move the UpdatePlayerList() to the bottom of the script?

1 Like

Just figured this out haha. Looks like Lua isn’t like JavaScript where you can define functions after you call them. :sweat_smile:

1 Like