How do I create a check to see if a level is higher than another..?


  1. I’m attempting to create an overhead GUI for a group that has other groups linked to it. (Sub groups would come in real handy right now.)

  2. The issue is when I go to load in-game, I always get a lower ranked group compared to the others.
    image

  3. I genuinely don’t know what solution to try, I feel like this was done correctly and I’m just missing a key element to this.


Scripts

Settings Module
local Settings = {}

Settings.Admins = {
	["Groups"] = {
		["Vortex Special Ops"] = {
			GroupID = 254932,
			SubGroupsActive = true,
			Active = true,
			RankRequirement = 249,
			SubGroups = {
				["Highranks"] = {
					Level = 5,
					Active = true,
					SubGroupID = 280826,
					RankRequirement = 1,
				},
				["Airborne"] = {
					Level = 4,
					Active = true,
					SubGroupID = 286052,
					RankRequirement = 1,
				},
				["Rancor Battalion"] = {
					Level = 3,
					Active = true,
					SubGroupID = 734442,
					RankRequirement = 100,
				},
				["Naval Forces"] = {
					Level = 2,
					Active = true,
					SubGroupID = 595668,
					RankRequirement = 253,
				},
				["Tags"] = {
					Level = .1,
					Active = false,
					SubGroupID = 595668,
					RankRequirement = 253,
				},
				["Investment"] = {
					Level = .2,
					Active = false,
					SubGroupID = 595668,
					RankRequirement = 253,
				},
				["Technicians"] = {
					Level = .3,
					Active = false,
					SubGroupID = 595668,
					RankRequirement = 253,
				},
			}
		}
		
	}
}



return Settings
Function Module, where to activate the functions
local Settings = require(script.Parent.Settings)
local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")
local Protocol = {}

Protocol.Functions = {
	["Overhead Check"] = {
		Description = "",
		Function = function(plr)
			for i,Group in pairs (Settings.Admins.Groups) do
				if Group.Active then
					if Group.SubGroupsActive then
						for index,SubGroup in pairs (Group.SubGroups) do
							if SubGroup.Active then
								if plr:GetRankInGroup(SubGroup.SubGroupID) >= 1 and game.Workspace:FindFirstChild(plr.Name) and game.Workspace[plr.Name].Head:FindFirstChild("VSOOverhead") and game.Workspace[plr.Name].Head.VSOOverhead.CurrentLevel < SubGroup.Level then
									local groupInfo = GroupService:GetGroupInfoAsync(SubGroup.SubGroupID)
									local info2send = {groupInfo.EmblemUrl,plr:GetRoleInGroup(SubGroup.SubGroupID),SubGroup.Level}
									return info2send
								elseif plr:GetRankInGroup(SubGroup.SubGroupID) >= 1 then
									local groupInfo = GroupService:GetGroupInfoAsync(SubGroup.SubGroupID)
									local info2send = {groupInfo.EmblemUrl,plr:GetRoleInGroup(SubGroup.SubGroupID),SubGroup.Level}
									return info2send
								end
							end
						end
					end
					if plr:GetRankInGroup(Group.GroupID) >= 1 and game.Workspace:FindFirstChild(plr.Name) then
						local groupInfo = GroupService:GetGroupInfoAsync(Group.GroupID)
						local info2send = {groupInfo.EmblemUrl,plr:GetRoleInGroup(Group.GroupID),0}
						return info2send
					elseif plr:GetRankInGroup(Group.GroupID) >= 1 then
						local groupInfo = GroupService:GetGroupInfoAsync(Group.GroupID)
						local info2send = {groupInfo.EmblemUrl,plr:GetRoleInGroup(Group.GroupID),0}
						return info2send
					end
				else
					return false
				end
			end
		end,
	}
}

return Protocol
Normal Script that gives the Overhead
local Admin = require(script.Parent.Parent.Modules.Functions)

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local GroupInfo = Admin.Functions["Overhead Check"].Function(plr)
		if char.Head:FindFirstChild("VSOOverhead") and char.Head.VSOOverhead.CurrentLevel < GroupInfo[3] then
			local GUI = char.Head.VSOOverhead
			GUI.Rank.Text = GroupInfo[2]
			GUI.Icon.Image = GroupInfo[1]
			GUI.CurrentLevel.Value = GroupInfo[3]
		else
			local GUI = script.VSOOverhead:Clone()
			print(GroupInfo)
			GUI.Rank.Text = GroupInfo[2]
			GUI.Icon.Image = GroupInfo[1]
			GUI.CurrentLevel.Value = GroupInfo[3]
			GUI.Parent = char.Head
		end
	end)
end)

Thank you for any assistance you can give me.

Why don’t you try using an array instead of a hash table for the rank list? It will make comparison much easier as now you can simply use ipairs on the table and iterate until the index is too high.

Here’s an example:

local ranks = { --the indices ("[1]", "[2]", etc.) are done for redundancy; you're better off without them since you can just insert new ranks easily without needing to change other ranks
    [1] = {Rank = 'Civilian', Info = 'noob :sob:'},
    [2] = {Rank = 'Janitor', Info = 'blablablabla'},
    [3] = {Rank = 'Staff', Info = 'blablablabla'},
    [4] = {Rank = 'Overseer', Info = 'pro :sunglasses:'},
}

function getRank(rank)
    for k, v in ipairs(ranks) do --iterate from least to greatest
        local nx = k + 1 --get the next rank of the current iteration
        if rank < nx or not ranks[nx] then --if the next rank doesn't exist, then that means the rank is already the highest available
            return v
        end
    end
end

print(getRank(2).Rank) --> Janitor
print(getRank(3.5).Rank) --> Staff
print(getRank(math.huge).Rank) --> Overseer
print(getRank(-1).Rank) --> Civilian

The reason I don’t do this is because, I have to give the GUI before finding the GUI. Plus, at the same time; wouldn’t I be able to use the value Level? Level is technically what the [1], [2], [3], [4] while being more simplified about what it is. This makes it easier for people who don’t truly understand Roblox Programming and make it easily editable for them.

In that case, I’ll refer to your original script. I haven’t read all of it (it’s quite a nightmare to do so) but it looks like you’re just directly comparing the rank to whatever pairs is giving to you first and going from there. This is a bad solution, since there is no definite order for iterating hash tables and you’re looking for whichever rank is higher than the player’s.
You will need to compare the player’s rank to ALL of the available ranks in that list and pick the highest that is acceptable.

example with what you wanted:

local ranks = {
    ['Civilian'] = {Level = -1},
    ['Janitor'] = {Level = .3},
    ['Staff'] = {Level = 1},
    ['Overseer'] = {Level = 10}
}

function getRank(rank)
    local highestLevel = -math.huge --initialize with smallest number
    local highestRank
    for k, v in pairs(ranks) do
        if v.Level > highestLevel and v.Level <= rank then --if it's higher than the chosen one BUT also less than or equal to the given rank
           highestLevel = v.Level --make it the next chosen one
           highestRank = k
        end
    end
    return highestRank
end

print(getRank(-100)) --> nil (no rank can satisfy this)
print(getRank(-1)) --> Civilian
print(getRank(.9999)) --> Janitor
print(getRank(.1)) --> Civilian
print(getRank(1.00001)) --> Staff
print(getRank(1e100)) --> Overseer
1 Like