My Script Admin Checking Script Isn't Working! Any Help?

My script that checks if a user is on a certain list seems not to work. Any help?!


local mps = game:GetService("MarketplaceService")
local users = game:GetService("Players"):GetChildren()

return function(allowed)
	if mps:UserOwnsGamePassAsync(game.CreatorId, 9122725) then
		print('AdminSystems™️ - Enterprise Loaded!')
		while wait(1) do
			for _, v in pairs(users) do
				if v.Name == allowed then
					if v.PlayerGui:WaitForChild("AdminGUI") then
						print("AdminSystems™️ - User Has GUI")
					else
						script.AdminGUI:Clone().Parent = v.PlayerGui
					end
				end
			end
		end
	else
		print('AdminSystems™️ - Game Owner Does Not Own The System! - Enterprise')
	end
end

What part of this doesn’t work? You cant just give us a whole script and ask us to fix it.

It doesn’t clone the GUI into the user’s PlayerGui.

I think it has something to do with this part:

Screen Shot 2020-04-25 at 3.02.29 pm

Alright so from investigation, it looks like you’re trying to make a system that gives the player a GUI when they join

Do not use loops for this
Use the event Players.PlayerAdded

An example of Players.PlayerAdded is

local function OnPlayerAdded(plr)
   print(plr.Name .. " joined the game")
end

Players.PlayerAdded:Connect(OnPlayerAdded)

(you can call the function whatever you want I just called it OnPlayerAdded for readability sake)

But since I’m using a ModuleScript, it doesn’t work if the first player joins and is on the array.

Array:
local allowed = {'Aiden_12114', 'Player2', 'Player3'}

Connect the event then iterate over Players:GetPlayers() by calling the function with said player

--Handle players already in the game
for _, v in pairs(Players:GetPlayers()) do
   OnPlayerAdded(v)
end

Players.PlayerAdded:Connect(OnPlayerAdded) --future event calls

I tried this:

local function OnPlayerAdded(plr)
			print(plr.Name .. " joined the game")
		end
		
		for _, v in pairs(allowed) do
			OnPlayerAdded(v)
		end
		Players.PlayerAdded:Connect(OnPlayerAdded)

But then I get this error:
Screen Shot 2020-04-25 at 3.08.46 pm

I also then tried

local function OnPlayerAdded(plr)
			script.AdminGUI:Clone().Parent = plr.PlayerGui
		end
		
		for _, v in pairs(allowed) do
			OnPlayerAdded(v)
		end
		Players.PlayerAdded:Connect(OnPlayerAdded)

But then nothing happens.
@FilteredDev

Do you get any infinite yield error in console with first source code you sent?

No, I don’t get anything in the console.

local function OnPlayerAdded(plr)
   if table.find(allowed, plr.Name) then
      script.AdminGUI:Clone().Parent = plr.PlayerGui
   end
end

Don’t iterate the allowed table in the for loop, use the return from Players:GetPlayers()

Thank you so much! I can now finally keep working on it! Thx :smiley: