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.