Scripting Help [Clearance Script]

So here is the script…

local Players = game.Players
local loaded = false
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:connect(function(char)
		local rank = Player:GetRankInGroup([GroupID)
		if rank == 0  or rank == 241 or rank == 242 then
			Player.Backpack.PrimaryClearance.Value = "CD"
		elseif rank == 244 then
			Player.Backpack.PrimaryClearance.Value = "L0"
		elseif rank == 245 then
			Player.Backpack.PrimaryClearance.Value = "L1"
		elseif rank == 246 then
			Player.Backpack.PrimaryClearance.Value = "L2"
		elseif rank == 247 then
			Player.Backpack.PrimaryClearance.Value = "L3"
		elseif rank == 248 then
			Player.Backpack.PrimaryClearance.Value = "L4"
		elseif rank == 249 then
			Player.Backpack.PrimaryClearance.Value = "L5"
			wait()
			if not Player.Backpack:FindFirstChild("Detain") then
				local Detain = script.Detain:Clone()
				Detain.Parent = Player.Backpack
			end
		elseif rank >= 251 then
			Player.Backpack.PrimaryClearance.Value = "Omni"
			wait()
			if not Player.Backpack:FindFirstChild("Detain") then
				local Detain = script.Detain:Clone()
				Detain.Parent = Player.Backpack
			loaded = true
			end
		end
	end)
end)

if loaded then
	print("Clearance Giver successfully loaded")
else
	print("Clearance Giver was not loaded")
end

And I want it to get rid of the rankID’s and replace it with teams as in…

Class-D Team will get a “CD” Clearance anyone know how to do this?

You’ll want to associate each team or team name with a list of ranks and also the “CD”, “L0” clearance names. It’ll be useful to have a table that just contains info about each team. Something like

local team_infos = {
	["Class-D"] = {
		clearance_name = "CD",
		group_ranks = {0, 241, 242},
	},
	["Level 0"] = {
		clearance_name = "L0",
		group_ranks = {244},
	},
	["Level 1"] = {
		clearance_name = "L1",
		group_ranks = {245},
	},
	["Level 2"] = {
		clearance_name = "L2",
		group_ranks = {246},
	},
	["Level 3"] = {
		clearance_name = "L3",
		group_ranks = {247},
	},
	["Level 4"] = {
		clearance_name = "L4",
		group_ranks = {248},
	},
	["Level 5"] = {
		clearance_name = "L5",
		group_ranks = {249},
		starter_pack = {"Detain"}
	},
	["Omni"] = {
		clearance_name = "Omni",
		group_ranks = {251},
		starter_pack = {"Detain"}
	},
}

You can then program functions that operate on this data. E.g. finding the team that a player should be put in, based on their group rank:

local function table_max(t)
	local highest_so_far

	for _, v in pairs(t) do
		if not highest_so_far then
			highest_so_far = v
		elseif v > highest_so_far then
			highest_so_far = v
		end
	end

	return highest_so_far
end	

function get_player_highest_rank_team( player )
	local rank = Player:GetRankInGroup(GroupID)

	local highest_so_far
	for _, team_info in pairs(team_info) do
		if not highest_so_far then
			highest_so_far = team_info
		else
			if table_max(team_info.ranks) > table_max(highest_so_far.ranks) then
				highest_so_far = team_info
			end
		end
	end

	return highest_so_far
end

And here’s the rest that’s needed to have something functionally equivalent to what you posted originally:

function give_player_tools( player, tool_names )
	for _, tool_name in pairs(tool_names) do
		local tool = script:FindFirstChild(tool_name)
		if tool and tool:IsA("Tool") then
			tool:Clone().Parent = player.Backpack
		else
			warn( ([[Could not find tool named "%s" in %s]]):format(
				tool_name,
				script:GetFullName()
			) )
		end
	end
end

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:connect(function(char)
		local player_team = get_player_highest_rank_team(player)
		give_player_tools(player, player_team.starter_pack)
		player.Backpack.PrimaryClearanceValue = player_team.clearance_name
	end)
end)

1 Like

Thanks, I was doing TeamColor and it broke the whole script lol.