Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to make a rank system that is based on a players level?

So I'm trying to make a rank system that's based on the player's level, but when I tried to make it it did not work. Is it even possible to do this, and if it's not is there any other way?

---//Error\---

ServerScriptService.LeaderStats.RankHandler:9: invalid argument #3 (string expected, got nil)

--//RankScript\---

---//Services\\---
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//ServiceChildren\\--
local USRankHandler = require(ReplicatedStorage.RankHandler:WaitForChild("USRankHandler"))
local VRankHandler = require(ReplicatedStorage.RankHandler:WaitForChild("VRankHandler"))
---//Script\\---
game.Players.PlayerAdded:Connect(function(player)
	if player.TeamColor == BrickColor.new("Cyan") then
		player:WaitForChild("leaderstats").Rank.Value = USRankHandler.Rank(player:WaitForChild("leaderstats2").Level.Value) ---Rank is a StringValue level is a IntValue

		player.leaderstats2.Level:GetPropertyChangedSignal("Value"):Connect(function()
			player:WaitForChild("leaderstats").Rank.Value = USRankHandler.Rank(player:WaitForChild("leaderstats2").Level.Value)
		end)
	end
end)

---//ModuleScript\---

local USRank = {}

local players = game.Players
local player = players.LocalPlayer

USRank.Rank = function(USR)
	
	if USR == 1 then
		USR = "Private"
			
	elseif USR == 2 then
		USR = "PFC"
			
	elseif USR == 3 then
		USR = "Corporal"
			
	elseif USR == 4 then
		USR = "Sergeant"

	elseif USR == 5 then
		USR = "Staff Sergeant"

	elseif USR == 6 then
		USR = "Technical Sergeant"

	elseif USR == 7 then
		USR = "First Sergeant"

	elseif USR == 8 then
		USR = "Master Sergeant"

	elseif USR == 9 then
		USR = "Second Lieutenant"

	elseif USR == 10 then
		USR = "First Lieutenant"

	elseif USR == 11 then
		USR = "Captain"

	elseif USR == 12 then
		USR = "Major"

	elseif USR == 13 then
		USR = "Lieutenant Colonel"

	elseif USR == 14 then
		USR = "Colonel"

	elseif USR == 15 then
		USR = "Brigadier General"

	elseif USR == 16 then
		USR = "Major General"

	elseif USR == 17 then
		USR = "Lt.General"

	elseif USR == 18 then
		USR = "General"

	elseif USR == 19 then
		USR = "General of the army"

	elseif USR >= 20 then
		USR = (USR).."CINC"
	end
end

return USRank

---//P.S\---

I'm new to module scripts. So please understand why I'm having a hard time with something like this.

2 comments

0

Well, I cant help you with your problem but I would suggest you use either an i, v in pairs loop or a while loop for that one section of your Module script that repeats many times.

0

So I put it before the if statment?

1 answer

✓ Accepted answer

All you're doing is modifying the local copy of "USR", which I assume means US-Rank. You never actually provided the determined result through a return statement.


####Extra Notes: 1). An elseif chain of that magnitude can be simplified to a Dictionary lookup. This will provide general ease with managing ranks, as well as a time-complexity of O(1) rather than O(n), which can be very costly.

2). The LocalPlayer is not required in this ModuleScript based off of context; if this is used on the Server, contradictions with its allocation will arise.

3). The leaderstats folder isn't going anywhere, you do not require a constant :WaitForChild call. If you're making reference to an Instance numerous times, just store it in a variable.


--###----------[[VARIABLES]]----------###--
local USRanks = {

	"Private",
	"PFC",
	"Corporal",
	"Sergeant",
	"Staff Sergeant",
	"Technical Sergeant",
	"First Sergeant",
	"Master Sergeant",
	"Second Lieutenant",
	"First Lieutenant",
	"Captain",
	"Major",
	"Lieutenant Colonel",
	"Colonel",
	"Brigadier General",
	"Major General",
	"Lieutenant General",
	"General",
	"General of the Army";

}



--###----------[[SETUP]]----------###--
return {

	Rank = function(MemberRank)
		if (MemberRank >= 20) then
			return MemberRank .. "CINC"
		end
		---------------
		return USRanks[MemberRank]
	end

}

1 comment

0

Thank you for the help! It worked, and I appreciate that!

This discussion is preserved as it was. New answers, comments, and votes are closed.