Why wont the Admin-Panel Hide script work?

Hi! I am trying to make a Admin-Panel that is supposed to be hidden and destroyed when the player isnt a team member. If the player is a admin then the GUI should clone itself and go to the admins PlayerGui.
But if the player isnt an admin and the GUI is still visible and there, it should kick the player and say: “An Error happened. Please rejoin!” Does anybody know how I can make the scripts better? It should work with a Remote Event, or maybe there is annother way to do it? Also when I look at the OutPut it does not show that the Remove Event is firing.

Localscript in the GUI > Frame:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CheckIfAdmin = ReplicatedStorage:WaitForChild("CheckPlayer")
wait(5)
game.Players.PlayerAdded:Connect(function()
	wait(1)
	CheckIfAdmin:FireServer()
end)

Script in ServerScriptService: (Only the GUI Hiding script)

local Admins = { 
	1124148345,
}

local Players = game:GetService("Players")
local CheckPlayer = game.ReplicatedStorage:FindFirstChild("CheckPlayer")


CheckPlayer.OnServerEvent:Connect(function(player)
	warn("The CheckPlayer Event got fired")
	player.PlayerGui.AdminPanel:Destroy()
	wait(1)
	if table.find(Admins, player.UserId) then
		
		local clone = game.StarterGui.AdminPanel:Clone()
		clone = player.PlayerGui
		wait(1)
		elseif not table.find(Admins, player.UserId) and player.PlayerGui.AdminPanel.Visible == true then
			player:Kick([[Uh oh! An error accured!
						Please report this error to the
						-A Chaotic Camping Trip- group!
						We are sorry for this error.
						please rejoin.]])
		
		
	end
end)

am I forgetting something about the local scripts and normal scripts? Or did I do something wrong about the Remote Event?

I would be thankful for everyone helping me!

Hello,

Do you get any errors in the Console output? This might help solve the problem.

1 Like

nope, theres nothing in the output. But somehow the GUI disapeared for me when somebody else joined.
It isnt supposed to disapear for me. I think it wont even fire when I am alone on the server

It fires whenever a player joins. Consider removing the function & turning off the “ResetOnSpawn” ScreenGUI property.

1 Like

The code overall could be done easier.

Why don’t you just make a ServerScript that connects to a player join function? Once a place joins, it checks if the user is in the Admin list, and if so Clones a copy of the Admin UI into the Users PlayerGUI.

for your code I see following problems:
1. You run the Script every time a player joins on everyone.
2. The function (due to it being a local Script) does only connect to .PlayerAdded on players that join after the first one joined. Every new user executes the code also once a player join leading to the Event being Fired multiple times by the user its self.
3. Due to the Event Firing every time, you get conflicts.

1 Like

Note: If you consider the clone version and want the UI to reset if a player respawns use the characterAdded instead of playerAdded function.

1 Like

should I swap the scripts? What I mean by that is putting the GUI Hiding script into the localscript and putting the game.PlayerAdded:Connect(function() script into the normal script?
Also should I use this instead?

game.Players.PlayerAdded:Connect(function(player)
wait(1)
CheckIfAdmin:FireClient(player)

would that be better? And can you explain to me how I can use characterAdded please?

Just gonna help you code better:

Do not use wait(), it will be deprecated soon, instead use task.wait()
Do not do Player.PlayerGui, use Player:WaitForChild("PlayerGui"). Why?
Sometimes the player can be added, but the PlayerGui object will not be in the player yet, making the script error.
You should store the GUI in ServerStorage.

1 Like

Why is your LocalScript using the PlayerAdded event? You should just check the LocalPlayer and remove or add the GUI if they are someone with or without permission. There is no reason to listen for PlayerAdded in this case.

It would be better to keep your AdminPanel GUI in ServerStorage to keep it safe from users, then distribute it when the server checks added users have permissions. It’s better to start a user off with no permissions assumed in almost all applications.

1 Like

As already mentioned, the current way is impractical.
I’ll recommend cloning a copy of the UI once a player is authorized.

i think an easier way would be this:

local Admins = {
	1124148345,

}

local player = game.Players.LocalPlayer

for i,v in pairs(Admins) do
	if player.UserId == v then
		script.Parent.Visible = true
	else
		script.Parent.Visible = false
	end
	
	while true do
		wait()
		if script.Parent.Visible == true and player.UserId ~= v then
			player:Kick([[Uh oh! An error accured!
						Please report this error to the
						-A Chaotic Camping Trip- group!
						We are sorry for this error.
						please rejoin.]])
		end
	end
end

Also, this script should be a LocalScript inside the Admin Panel. To be honest, this doesn’t really require 2 scripts for this! Please reply to me if this doesn’t work, i’ll be sure to help!

1 Like

Sry for the long waiting. I didnt expect that anyone would still answer to this post, thats why I have completly forgot about this post. Anyways your script works. Thanks for the help! I am gonna modify it a bit so it can be a little bit more secure because exploiters might just make it visible and then use it to ban players.

No problem! When I scripted this, I wasen’t familiar with lua so it is kinda easy to bypass. You could use the .Changed function to see if it became visible, like


local Admins = {
	1124148345,

}

local player = game.Players.LocalPlayer

for i,v in pairs(Admins) do
	if player.UserId == v then
		script.Parent.Visible = true
	else
		script.Parent.Visible = false
	end
	
	script.Parent.Changed:Connect(function()
		if script.Parent.Visible == true and player.UserId ~= v then
			player:Kick('[[Uh oh! An error accured!
						Please report this error to the
						-A Chaotic Camping Trip- group!
						We are sorry for this error.
						please rejoin.]]')
		end
	end
end

1 Like