Admin Panel fails to detect player

Hi there.
I’ve been having this pesky problem with my admin panel recently.
How this admin panel works is that it is enabled when joined. If the player still has the admin panel, and presses one of the buttons it gets kicked.
When a new player joins the server, I run a function that adds them to the players frame which makes a new button. When the player button is selected, it triggers a remote event that sets a global variable to the player selected.
If you try to run an administrative event, it wont work if the _G variable is set to nil. When the player is selected it is set to that player.

My current problems are:

  • remote event doesnt even get called
  • remove event gets fired but server doesnt see anything
  • button is pressed which is supposed to fire the remote but it never fires
  • when player is removed gui is never updated

I do make the player buttons locally, but I set the variable on the server. I will be actively trying to fix this as I make this post. Any fixes to my problem will be extremely helpful!

Local Script inside of Players Frame
local players = script.Parent
local playerService = game:GetService("Players")
local event = game:GetService("ReplicatedStorage"):FindFirstChild("SelectedPlayer")

local function addPlayer(player)
	local playerUi = script.Parent.Parent.Placeholder:Clone()
	playerUi.Parent = players
	playerUi.Name = player.Name
	playerUi.ImageTransparency = 0
	
	local textLabel = playerUi.TextLabel
	textLabel.Text = player.Name
	textLabel.TextTransparency = 0
	textLabel.Name = player.Name
end

local function removePlayer(player)
	print(script.Parent)
	for i, v in pairs(script.Parent:GetChildren()) do
		print(v.Name)
	end
	local players = script.Parent:GetChildren()
	for i, player1 in pairs(players) do
		if player1:IsA("ImageLabel") then
			if player.Name == player1 then
				player1:Destroy()
				return
			end
		end
	end
end

playerService.PlayerAdded:Connect(addPlayer)
	for _, player in pairs(playerService:GetPlayers()) do
	addPlayer(player)
end


playerService.PlayerRemoving:Connect(function(player)
	local name = player.Name
	print(name)
	removePlayer(name)
end)



for _, playerButton in pairs(players:GetChildren()) do
	if playerButton:IsA("ImageButton") then
		playerButton.MouseButton1Click:Connect(function()
			event:FireServer(playerButton.Name)
			print("Event invoked.")
		end)
	end
end
Server Script
local RS = game:GetService("ReplicatedStorage")
local trigger = RS:FindFirstChild("PromptCommand")
local selected = RS:FindFirstChild("SelectedPlayer")

local admins = require(script.Parent.Admins)
local commands = require(script.Parent.Commands)
local Players = game:GetService("Players")

selected.OnServerEvent:Connect(function(player, selected)
	local admin = admins.CheckAdmin(player)
	if not admin then
		player:Kick("Accessing admin gui without admin.")
	else
		commands.selectedPlayer = selected
		print(commands.selectedPlayer)
		local suc, err = pcall(function()
			local plr = Players:FindFirstChild(commands.selectedPlayer)
			local content, ready = Players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
			player.PlayerGui.AdminPanel.PlayerSelected.ImageLabel.Image = content
		end)
		
		if suc then
			print("selected")
		else 
			warn(err)
		end
	end
end)

trigger.OnServerEvent:Connect(function(player, command)
	local isAdmin = admins.CheckAdmin(player)
	if isAdmin then
		if commands.selectedPlayer ~= nil then
			commands[command](commands.selectedPlayer)
			print("not select")
		end
	else
		player:Kick()
	end
end)

Players.PlayerAdded:Connect(function(plr)
	local admin = admins.CheckAdmin(plr)
	if admin then
		plr.PlayerGui:WaitForChild("AdminPanel").Enabled = true
		print("Admin panel activated.")
	end
end)
Location

Capture

In a one player server, the kick command (only one i’ve made) works. Introduce more than one player and it breaks.
No errors were given but I have print statements that don’t run which I will provide after I edit this.

4 Likes

This might be a part of the problem:

local players = script.Parent

local players = script.Parent:GetChildren()

On the event variable I would also do WaitForChild instead of FindFirstChild:

local event = game:GetService("ReplicatedStorage"):WaitForChild("SelectedPlayer")

A big part of your problem is you set up the function for the player buttons before any buttons are created and it doesn’t loop so the buttons won’t do anything due to being created after, a good workaround would be to put the function in the button creation as shown:

Local Script inside of Players Frame
local players = script.Parent
local playerService = game:GetService("Players")
local event = game:GetService("ReplicatedStorage"):FindFirstChild("SelectedPlayer")

local function addPlayer(player)
	local playerUi = script.Parent.Parent.Placeholder:Clone()
	playerUi.Parent = players
	playerUi.Name = player.Name
	playerUi.ImageTransparency = 0
	
	local textLabel = playerUi.TextLabel
	textLabel.Text = player.Name
	textLabel.TextTransparency = 0
	textLabel.Name = player.Name
        playerUi.MouseButton1Click:Connect(function()--when the button gets created so does the click function
			event:FireServer(playerUi.Name)
			print("Event invoked.")
		end)
end