So I have been struggling for the past 30 minutes trying to make this clone UI to player.PlayerGui and seems like it doesn’t want to work and barely giving me any error details to fix it with can someone help me with this?
local Admins = {
968839, -- CLarramore
293826824, -- ca00000
52260945, -- Stormidly
993731761, -- My Alt
145068339, -- Me
76669491, -- Fyris54
562491343, -- KingWilliam234
457147594, -- BlushingBoy
101291900, -- Suilerbean03
823284864, -- BlueShallowFox
533985741} -- Cheesebelife
function isAdmin(player)
for i, v in pairs(Admins) do
if player.UserId == v or player:GetRankInGroup(4813882)>=250 then
script.Parent.AdminPanel:Clone().Parent = player.PlayerGui
end
end
end
Other than a couple of potential optimizations, I don’t see anything wrong with your code, or at least anything technical enough to call it out. Where are you using the isAdmin function?
From what is here, your code should work fine but I do not see anywhere of you calling the isAdmin function in the scripting provided. Are you calling the function because without calling on the function you are not able to run it and receive the admin panel.
Conventionally, a function named isX expects to return a true or false value based on a few conditional checks. It’s a little weird to call it isAdmin but use it to clone Guis. That’s mostly a matter of preference though and I felt like pointing that out.
There are a few optimisation tips I can recommend here:
Use ipairs. To my knowledge, it runs faster than pairs and you’re only using the array part of a table. You don’t have any need to know indexes either, so just opt for ipairs.
Separate the UserId check from the group check. In each iteration of the loop, you’re repeating a web call that checks if the player’s UserId matches the current selected UserId for the iteration or if they’re above a certain rank. You don’t need to repeat the group check so many times. This also helps minimalise web traffic in your game.
Run the group check first. When you run the group check first, that compares against one condition and checks whether its valid or not. It also helps reduce how many times you loop, since if an admin holds the needed rank then they skip UserId check. If not, then it traverses through your Admin list to see if the UserId is found.
Last thing - make sure to break loops that don’t need to run anymore. Whenever you check for an admin, the script will always run the entire list. Iterations over the list should stop the moment that a condition is met. In my code sample below, I’m using return since I changed how isAdmin worked. Return will automatically exit out of a loop and the function scope.
local Admins = {
-- repeat your list here
}
local function isAdmin(player)
for _, userId in ipairs(Admins) do
if player.UserId == userId then
return true
end
end
return false
end
local function giveAdminGui(player)
if player:GetRankInGroup(4813882) >= 250 or isAdmin(player) then
script.Parent.AdminPanel:Clone().Parent = player.PlayerGui
end
end
Don’t forget to call your functions! If you need this to happen as in when a player joins, I recommend writing a brief handler for it. This segment assumes ResetOnSpawn is true for the Gui. If it’s false, just have it work with CharacterAdded. Be wary of potential memory leaks and all.
local Players = game:GetService("Players")
local function onPlayerAdded(player)
giveAdminGui(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in ipairs(Players:GetPlayers()) do
onPlayerAdded(player)
end