My exp system isnt working

Trying to return the rank name if they have the correct exp for it and Im just getting errors.

The Code:

local ranks = {}
ranks.getRank = function(experince)
	local exp = tonumber(experince)
	local GameRanks = require(game.ReplicatedStorage.Modules.RankList)
	if exp >= tonumber(GameRanks[exp]) then
		return GameRanks[exp].Rank
	end
end
print(ranks.getRank("0"))
return ranks

GameRanks:

local module = {
	["0"] = {
		Rank = "Private"
	},
	["100"] = {
		Rank = "Private First Class"
	}	
	
}
return module

it would make things easier if you included said errors .-.
edit: and what line the error is coming from since they might not be the same line as in the post

the error is trying to compare nil and a number, so after

please put print(experience, exp, tonumber(GameRanks[exp])) and tell me if either is nil

Either stop using tonumber or give RankList numerical keys and not strings. RankList’s keys are strings but you’re trying to index values in it with a number.

local module = {
	[0] = {
		Rank = "Private"
	},
	[100] = {
		Rank = "Private First Class"
	}	
	
}
return module

What I see is on line 5 you call tonumber(GameRanks[exp]) but GameRanks[exp] is a dictionary therefore tonumber returns nil.

13:21:38.728 - local ranks = {}
13:21:38.729 - ranks.getRank = function(exp)
13:21:38.729 - 	local GameRanks = require(game.ReplicatedStorage.Modules.RankList)
13:21:38.729 - 	if exp >= GameRanks[exp] then
13:21:38.729 - 		return GameRanks[exp].Rank
13:21:38.730 - 	end
13:21:38.730 - end
13:21:38.730 - print(ranks.getRank("0"))
13:21:38.730 - return ranks:4: attempt to compare table and string

Here you’re trying to compare exp which is a string to one of the values in the dictionary which are all tables, if you want it to find the highest rank below your exp you can loop through everything in RankList and find which one is directly under it.

local YourRankId = 0
for i, rank in pairs(GameRanks) do
    if i > YourRankId and i <= exp then
        YourRankId = i
    end
end
local Rank = GameRanks[YourRankId]

I haven’t tested this and there may be a much better method

13:30:09.535 - 	return GameRan:6: attempt to compare number and string
local ranks = {}
ranks.getRank = function(exp)
	local GameRanks = require(game.ReplicatedStorage.Modules.RankList)
	local YourRankId = 0
	for i, rank in pairs(GameRanks) do
		if i > YourRankId and i <= exp then
			YourRankId = i
		end
	end
	return GameRanks[YourRankId].Rank
end
print(ranks.getRank("0"))
return ranks

I said to either stop using tonumber or give your dictionary numerical keys, not both.

exp = tonumber(exp)

Do this to convert exp to a number like you were doing earlier.
Or you could just remove the quotes from the 0 in print like the guy under me said

He could just remove the apostrophes in print(ranks.getRank("0")) and that’ll fix the issue.

13:33:34.748 - local ranks = {}
13:33:34.748 - ranks.getRank = function(exp)
13:33:34.748 - 	local GameRanks = require(game.ReplicatedStorage.Modules.RankList)
13:33:34.749 - 	local exp = tonumber(exp)
13:33:34.749 - 	local YourRankId = 0
13:33:34.749 - 	for i, rank in pairs(GameRanks) do
13:33:34.749 - 		if i > YourRankId and i <= exp then
13:33:34.749 - 			YourRankId = i:7: attempt to compare number and string

local ranks = {}
ranks.getRank = function(exp)
local GameRanks = require(game.ReplicatedStorage.Modules.RankList)
local exp = tonumber(exp)
local YourRankId = 0
for i, rank in pairs(GameRanks) do
if i > YourRankId and i <= exp then
YourRankId = i
end
end
return GameRanks[YourRankId].Rank
end
print(ranks.getRank(“0”))
return ranks

print(ranks.getRank(0))

If it still errors then your dictionary’s keys are still strings.

Same error.

local ranks = {}
ranks.getRank = function(exp)
	local GameRanks = require(game.ReplicatedStorage.Modules.RankList)
	local YourRankId = 0
	for i, rank in pairs(GameRanks) do
		if i > YourRankId and i <= exp then
			YourRankId = i
		end
	end
	return GameRanks[YourRankId].Rank
end
print(ranks.getRank(0))
return ranks
local module = {
	[0] = {
		Rank = "Private"
	},
	[100] = {
		Rank = "Private First Class"
	}	
	
}
return module

Let me test this out in studio, I may have just made a dumb mistake

I just tested this out in studio and it worked fine.
image
I used the code you just gave

module1 (named RankList)

local module = {
	[0] = {
		Rank = "Private"
	},
	[100] = {
		Rank = "Private First Class"
	}	

}
return module

module 2 (just named “s”)

local ranks = {}
ranks.getRank = function(exp)
	local GameRanks = require(game.ReplicatedStorage.Modules.RankList)
	local YourRankId = 0
	for i, rank in pairs(GameRanks) do
		if i > YourRankId and i <= exp then
			YourRankId = i
		end
	end
	return GameRanks[YourRankId].Rank
end
print(ranks.getRank(0))
return ranks

Requiring script

require(game.ReplicatedStorage.Modules.s)
1 Like