Creating an intermediate-advanced Admin System

How could I make something like:

Function.command.slock = function(speaker, args)
	local rank = args[1]
	if rank < 1 or rank > 5 then
		rank = 4
	end
	main.ranksAllowedToJoin = rank
	for i, plr in pairs(main.players:GetChildren()) do
		main.signals.Hint:FireClient(plr, {"Standard", "The server has been locked for ranks below '".. main:GetModule("cf"):GetRankName(rank).."'", Color3.fromRGB(255,255,255)})
	end
end;
Function.commands.unslock = function(speaker, args)
	main.ranksAllowedToJoin = 0
	for i, plr in pairs(main.players:GetChildren()) do
		main.signals.Hint:FireClient(plr, {"Standard", "The server has been unlocked", Color3.fromRGB(255,255,255)})
	end
end;
--
};
2 Likes

So a sever lock? I would just have a locked variable at the top of the module, something like this:

local Commands = {}
local serverLocked = false 

Now for the slock functions I would make something like this:

local Commands = {}
local serverLocked = false 

function Commands.slock(player)
	if hasRank(player, 2) then
		serverLocked = true 
	end
end

function Commands.unslock(player)
	if hasRank(player, 2) then
		serverLocked = false
	end
end

I would connect a function to the PlayerAdded event that checks if his rank is enough for him to join, something like this:

Players.PlayerAdded:Connect(function(player)
	if serverLocked and not hasRank(player, 2) then
		player:Kick("Server is locked.")
	end
end)

Now if you want to make it so only certain ranks and above can join, you can set it to a rank instead of true/false.

local Commands = {}
local serverLocked = false 

function Commands.slock(player, rankNeeded)
	if rankNeeded then 
		rankNeeded = tonumber(rankNeeded)
		if hasRank(player, 2) and hasRank(player, rankNeeded) then --he shouldnt be able to lock the server for admins if he is only a mod so that's why the second check
			serverLocked = rankNeeded
		end
	end
end


function Commands.unslock(player)
	if serverLocked and hasRank(player, serverLocked) then
		serverLocked = false
	end
end

and as function connected to the PlayerAdded event you would have something like this:

Players.PlayerAdded:Connect(function(player)
	if serverLocked and not hasRank(player, serverLocked) then
		player:Kick("Server is locked.")
	end
end)

If he doesn’t have the required rank to join, he gets kicked.

1 Like

Okay thanks!
Sorry I didn’t reply.

1 Like

Sorry it took me a while but its not working is this correct?

local Commands = {}
local Players = game:GetService("Players")
local serverLocked = false 

Players.PlayerAdded:Connect(function(player, hasRank)
	if serverLocked and not hasRank(player, serverLocked) then
		player:Kick("Server is locked.")
	end
end)

local function getPlayerByPlayerName(name)
	if name then --name could be nil (player forgot to include the parameter)
		for i,v in ipairs(Players:GetPlayers()) do
			if string.lower(string.sub(v.Name, 1, #name)) == string.lower(name) then
				return v
			end
		end
	end
end

local ranksForRole = {
	Guest = 1,
	Mod = 2,
	Admin = 3,
    ["CO-Owner"] = 5,
	Owner = 6
}
local function hasRank(player, needed)
	print(player:GetRoleInGroup(5199605))
	local rank = ranksForRole[player:GetRoleInGroup(5199605)]
	if rank >= needed then
		return true
	end
end

function Commands.slock(player, rankNeeded)
	if rankNeeded and hasRank(player, 2) and hasRank(player, rankNeeded) then --he shouldnt be able to lock the server for admins if he is only a mod so that's why the second check
		serverLocked = rankNeeded
	end
end

function Commands.unslock(player)
	if hasRank(player, serverLocked) then
		serverLocked = false
	end
end

function Commands.shutdown(plr, reason)
	if hasRank(plr, 4) then
		reason = reason or "No reason given."
		for i,v in ipairs(Players:GetPlayers()) do
			v:Kick("Server shutdown. Reason: " .. reason)
		end
	end
end

function Commands.to(player, toPlayerName)
	if hasRank(player, 2) then
		local plrToTeleportTo = getPlayerByPlayerName(toPlayerName)
		if plrToTeleportTo then
			player.Character.HumanoidRootPart.CFrame = plrToTeleportTo.Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, 5) --you can use CFrame.Angles to rotate the player too so he is looking at him
		end
	end
end

function Commands.F3X(plr, to)
	if hasRank(plr, 2) then --Mods and above
		local plrToGiveToolsTo

		if to and string.lower(to) == "me" then
			plrToGiveToolsTo = plr
		else
			print("Yeah no do me")
		end

		if plrToGiveToolsTo then
			for i,v in ipairs(game:GetService("ServerStorage").BuildingTools:GetChildren()) do
				v:Clone().Parent = plrToGiveToolsTo.Backpack
			end
		end
	end
end

function Commands.giveF3X(plr, to)
	if hasRank(plr, 2) then --Mods and above
		local plrToGiveToolsTo

		if to and string.lower(to) == "me" then
			plrToGiveToolsTo = plr
		else
			plrToGiveToolsTo = getPlayerByPlayerName(to)
		end

		if plrToGiveToolsTo then
			for i,v in ipairs(game:GetService("ServerStorage").BuildingTools:GetChildren()) do
				v:Clone().Parent = plrToGiveToolsTo.Backpack
			end
		end
	end
end

function Commands.bring(player, plrToBring)
	if hasRank(player, 3) then
		local plrToBring = getPlayerByPlayerName(plrToBring)
		if plrToBring then
			plrToBring.Character.HumanoidRootPart.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, 5)
		end
	end
end

function Commands.kick(player, plrToKick, reason)
	if hasRank(player, 3) then
		local plrToKick = getPlayerByPlayerName(plrToKick)
		if plrToKick then
			plrToKick:Kick("You were kicked. Reason: " .. reason)
		end
	end
end

return Commands

image cuz i get this error

2 Likes

Oops, you have to run tonumber on rankNeeded as it’s a string, looks like Roblox doesn’t do that automatically (even though I had in memory that it does).

Here the corrected functions:

function Commands.slock(player, rankNeeded)
	if rankNeeded then 
		rankNeeded = tonumber(rankNeeded)
		if hasRank(player, 2) and hasRank(player, rankNeeded) then --he shouldnt be able to lock the server for admins if he is only a mod so that's why the second check
			serverLocked = rankNeeded
		end
	end
end

function Commands.unslock(player)
	if serverLocked and hasRank(player, serverLocked) then --check if server is even locked and if his rank is higher
		serverLocked = false
	end
end

If there are any more issues feel free to ask here and I or somebody else will probably answer it.

2 Likes

:eyes: can this be used for groups? Asking for uh… well… just answer it…

1 Like

Yes, I’m showing how to create a hasRank function that checks if the player has the needed rank.

1 Like

Ok. That’s all I needed to know. And why are u up at 5:27 am EST? Why am I up?

2 Likes

Quick tip on how to do certain admin levels:

local Admins = {
	Groups = {
		{GroupID=000000,RankID=36,Level=5};
	};
	
	Players = {
		{Username="Noob",UserID=1,Level=5};
	};
};

local function FindLevel(plr)
	for i,admin in pairs(Admins.Groups) do
		if plr:GetRankInGroup(admin.GroupID) == admin.RankID then
			return tonumber(admin.Level)
		end
	end
	
	for i,user in pairs(Admins.Players) do
		if plr.UserId == user.UserID then
			return tonumber(user.Level)
		end
	end
	
	return tonumber(0) or 0 or nil
end

Command Example:

function Commands.test(player, args)
	if FindLevel(player) >= tonumber(1) then
		warn("Works!")
	end
end

Quick tip about the “player, args”, I simply changed the table.unpack function in the server script to args. I changed the “splits” to “args”. Example:

local Players = game:GetService("Players")
local Commands = require(script.Commands)
local Prefix = ":"

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if string.sub(message, 1, #Prefix) == Prefix then
			local messageWithoutPrefix = string.sub(message, #Prefix + 1, #message)
			local args = string.split(messageWithoutPrefix, " ")
			local firstWord = string.lower(args[1])

			if Commands[firstWord] then
				table.remove(args, 1)
				Commands[firstWord](player, args)
			end
		end
	end)
end)
1 Like

Very late. Is this up-to-date?

1 Like