Lua: there is an admin in the server?

Hello.

Excuse me for any grammatical errors.

I would be like to make a system that automatic starts a script when in the server there isn’t any MOD/admin/s.admin.

I think I have to do something like this:



local staff

for k, v in pairs(player.GetAll()) do
   v:GetUserGroup() 
   if v:IsUserGroup( "mod", "admin", "superadmin" ) then --I think this is an error!
	staff = 1
   else
	staff = 0
   end
end

if staff then
   *THE SCRIPT*
end


Someone can help me?

I now this is a very bad code.

Your code isn’t far off from working, the problem is the bit



 v:GetUserGroup()


That doesn’t need to be there, also, IsUserGroup only accepts one string (from what I can tell from the wiki), so you’ll probably going to have to break it up like this:



if v:IsUserGroup( "mod" ) or v:IsUserGroup( "admin" ) or v:IsUserGroup( "superadmin" ) then


Also, you seem to be wanting to run the script when there ISN’T staff, but in your code, you are. You might want to change that

[editline]2nd May 2016[/editline]

Also, if you want the code to automatically run, it might be a good idea to put it in a hook like

GM:Initialize

[editline]2nd May 2016[/editline]

I changed your code a bit and came up with this:



hook.Add( 'Initialize', 'some_unique_name', function()

	local staff = false

	for k,v in pairs( player.GetAll() ) do
		if v:IsUserGroup( "mod" ) or v:IsUserGroup( "admin" ) or v:IsUserGroup( "superadmin" ) then
			staff = true
		end
	end

	if !staff then -- if staff is false, meaning there are no staff then
	   -- run the script
	end

end )


Theoratically, I think it’d work

You could also try using

Player:IsAdmin

Oh, thank you!
Yes that hook is perfect for me!

[lua]local groups = {
mod = true
}

local adminOnline

for k, v in pairs(player.GetAll()) do
if (v:IsAdmin() or groups[v:GetUserGroup()]) then
adminOnline = true
break
end
end

if (adminOnline) then

end[/lua]

:snip: automerge

Thank you very much!

:snip: damn automerge

I’m using this, but it don’t works!
I’m superadmin and the script starts anyway!

Oh, it’s cause I’m an idiot, sorry. Remove the ‘else staff = false’ bit, that must be resetting the staff variable whenever someone on the server ISN’T a superadmin/admin

[editline]2nd May 2016[/editline]

I updated the thing from before

Am i the only one thinking the groups inside that table should be in quotation marks?

Only needed if the usergroup has spaces, symbols, or other variable name limitations. Most usergroups don’t have those anyways. It wasn’t a conscious thing I did. If anyone wants to use it with quotes, go for it. If I know that none of the groups will have those limitations I like to write it that way.

If you are the only one, do I click “agree” or “disagree”?

There are 4 valid syntax options:
[lua]local groups = {
mod = true
}

local groups = {
[“mod”] = true
}

local groups = {}
groups.mod = true

local groups = {}
groups[“mod”] = true

– wrong, syntax error:
local groups = {
“mod” = true
}[/lua]