Is this "rankchecker" good?

Hello programmers!

Introduction and 'Problem'

I’m here to ask for a bit of feedback on the custom “Rankchecker” that I created. The idea is to simply check the rank of a player, but because HTTP 500 can happen every now and then, I tried of thinking of a solution using caching.

Now, I’m not sure if it actually works, and I’m not that tedious to request a player’s rank until I get a HTTP 500 error. Therefore, I’d like your opinion on this code and possible feedback. If it turns out to work, then feel free to use this script for your own personal projects. It’s nothing major, so I don’t mind you using it if you want to use it.

Now, without further ado, here’s the code: (Note, it’s in a ModuleScript)

local module = {}

local Cache = {}

module.GetRank = function(Player: Player,GroupId: number)
	
	--Check if arguments are not nil
	if not Player or not GroupId then
		
		warn("Either the Player or the GroupId was not given.")
		return
	end
	
	--Get the rank
	local Rank
	
	local Success, Err = pcall(function()
		
		Rank = Player:GetRankInGroup(GroupId)
	end)
	
	if Success and Rank ~= nil then
		
		if not Cache[Player.Name] then
			
			Cache[Player.Name] = {}
			Cache[Player.Name][GroupId] = Rank
		end
		return Rank

	else --HTTP 500 Error. Let's try to see if there is any cached data on the player's rank.
		
		if Cache[Player.Name] then
			
			if Cache[Player.Name][GroupId] then
				
				return Cache[Player.Name][GroupId]
			else
				
				task.wait(3)
				module.GetRank(Player,GroupId) --Try again. Kinda desperate, but could work.
			end
		else
			
			task.wait(3)
			module.GetRank(Player,GroupId) --Try again. Kinda desperate, but could work.
		end
	end
end

return module

As you can see from the script, I always try and get the freshest data on somebody’s rank, incase they get a new rank while they are in-game, they won’t need to rejoin to get the new rank.

Yes I know

I know that I can simply return a function since it’s just a single function for checking the rank, but for the future, incase I need to add more functions, I could simply create another function easily.

2 Likes

I think a good Idea is maybe every 60 seconds you just check everyones rank again and update it that way.

I don’t really like the idea of having it time based. I’d rather have it done whenever you click or really need it.

Edit: It doesn’t really help me check if it actually works or not when a HTTP 500 error occurs.

Can’t you just use data stores for caching?

The reason why I cache is because I want to find a “solution” to HTTP 500. By utilizing datastores, I would run in the same problem.

Besides, using datastores wouldn’t really be caching then, but rather saving.

Caching the rank of the player will not stop HTTP 500 issues. If you aren’t sure if it actually works, then why have you posted this here for code review when the module it self doesn’t even work properly to begin with? The entire code is flawed as well, instead this should rather be in #help-and-feedback:scripting-support.

And speaking of the caching system overall, it is redundant. Player:GetRankInGroup already caches the result…

Thanks for this sophisticated answer. I’ll try to improve on it then. Thanks!