ISAdmin Function isnt working

It keeps responding with i’m not admin somehow.

Code:

local function isAdmin(target, level)
		local rank = string.lower(level)
		if rank == "moderator" then
			for a,b in pairs(settings.Ranks.Moderators) do
				if target == b then
					return true
				else
					for c, d in pairs(settings.Ranks.Administrators) do
						if target == d then
							return true
						end
					end
				end
			end
		end
		
	end

Settings:

local settings = {
		["General"] = {
			Prefix = ":"
		},
		["Ranks"] = {
			Moderators = {},
			Administrators = {"blvckjakey"},
			Owners = {},
			Creators = {"blvckjakey"}
		}
		
	}

Whole Script:

local admin = {}

admin.run = function(...)
	local arguments = {...};
	local player = arguments[1]
	local settings = arguments[2]
	local prefix = settings.General.Prefix
	local function isAdmin(target, level)
		local rank = string.lower(level)
		if rank == "moderator" then
			for a,b in pairs(settings.Ranks.Moderators) do
				if target == b then
					return true
				else
					for c, d in pairs(settings.Ranks.Administrators) do
						if target == d then
							return true
						end
					end
				end
			end
		end
		
	end
	local function findTarget(input)
		input = tostring(input):lower()
		for _, player in ipairs(game:GetService("Players")) do
			if (player.Name:lower():sub(1, #input) == input) then
				return player
			else
				return "Player couldn't be found"
			end
		end
	end
	local function adminLog(command, admin, target)
		warn(command.." ran the command ".. command .." on ".. target)
	end
	player.Chatted:Connect(function(message)
		local msg = string.lower(message)
		if msg:sub(1, 8) == prefix.."respawn" then
			if (isAdmin(player.Name, "Moderator") == true) then
			local subLower = string.lower(msg:sub(9))
			if subLower == "me" then
				player:LoadCharacter()
			else
			local target = game.Players:FindFirstChild(msg:sub(9)) 
			if target == nil then
					if findTarget(msg:sub(9)) == "Player couldn't be found" then
							warn("Player could'nt be found")
						else
					local tempTarget = findTarget(msg:sub(9))
					tempTarget:LoadCharacter()
				end
			else
				target:LoadCharacter()
				end
				end
			else
				warn("Player isnt a admin, can't run this command")
			end
		end
	end)
end

return admin
1 Like

The problem is that it isn’t checking if you are an administrator. It appears to do this:

  • It only checks if the player is a given rank if that rank is “moderator”. So, it will always return false if the rank it is checking for is not “moderator”.

  • Let’s say the rank it is checking for is “moderator”. It’ll enter a loop and iterate through the list of moderators. If there is a match, it returns true.

    • Otherwise, it’ll iterate through the list of administrators and if there is a match there it’ll return true.
      • Otherwise, it returns back to the main loop and repeats. When it finishes iterating through the list of moderators (meaning no match found), it’ll return false.

The intention of the code is not clear at all. Is it supposed to check if you are an administrator, or is it supposed to check if you are a staff member?

Extra

You should not be using the player’s name to identify a player. You should use the UserId of a player instead of a player’s name as that cannot be changed. Additionally, you should clean up the code by doing the following:

  • Consistent casing for variables.
  • Naming functions more clearly
  • Consistent indenting
  • And talking to a duck about your code and what it does. It helps find out bugs!

I’m confused, could you show me example code of it working?

Which part are you confused about?

I’m confused on how its keep looping thru moderator and not moving onto administrator. Mind sharing example code? It’s suppose to check if I’m in the admin table even tho the requirement is mod, I’m above mod so It should search for me in the admin table.

I think this is what the code is supposed to do. The problem was that the way you named your function (plus its current behavior) confused me. I wasn’t sure if it was supposed to find if the player was an admin or a staff. But, here?

local function IsStaff(PlayerName)
    for _, RankList in next, settings.Ranks do
        for _, PossiblePlayers in next, RankList do
            if PossiblePlayer == PlayerName then
                return true
            end
        end
    end
    return false
end

I renamed it to make it align with what it actually does.

Extra details

The problem is that your ranks have no order. Moderator has no rank value that’d put it below Administrator. They’re technically the same place. What I mean by a order is this:

["Ranks] = {
    [1] = { Name = "Moderator", {"Someone's name"} };
    [2] = { Name = "Administrator", {"Else's name"} };
}

So, now it’s clear that an administrator is ranked higher than a moderator.

1 Like

Here’s something you can work with (?)

local Ranks =
{
	Admin = 
	{
		Allowed =
		{
			Userid1,
			Userid2
		},
		Rank = 2
	},
	
	Mod = 
	{
		Allowed =
		{
			Userid1,
			Userid2
		},
		Rank = 1
	}
}

-- You should probably store the numbers somewhere so it's easier to tell who's above who.

local function GetRank(Player)
	if table.find(Ranks.Admin.Allowed, Player.UserId) then
		return Ranks.Admin.Rank -- return their number? idk what u wanna do.
		
	elseif table.find(Ranks.Mod.Allowed, Player.UserId) then
		return Ranks.Mod.Rank
	end
end


local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(Player)
	print(GetRank(Player))
end)
1 Like