Hi, I was just wondering if I should handle this differently, or if it would be fine. Basically it updates your rank on leaderstats whenever you join the game and changes it when you rank up. It works fine, but I was just wondering if any problems could somehow pop up, maybe if someone joins and quickly leaves? idk.
This is a server script inside of serverscriptservice
Could definitely be improved but first what does the RANK value do? If it can become a decimal, I would use a range instead of just a number when checking the value. If not, what is the point of having the RANK value in the first place and not just saving the fakerank value in a string?
The player only sees the letter rank. There isn’t anything I would change about the system, I was simply wondering if the script I showed could somehow error if a player left quickly or something or cause an infinite yield
I don’t think that would cause any errors, although you could consider having that function handle the creation of the “leaderstats” folder as well as “Rank” and “PlrStats” objects in the case it is never created from another script in the game (or else the rest of the function would not run and the player’s “fakerank” Value would never be updated).
Aside from that, one other recommendation I’d make to improve the code would be to move each of the numbers and subsequent rank values into a table so it’s more organized and scalable if you wanted to reference those values from different functions / add more ranks in the future:
Example Revision
local Players = game:GetService("Players")
local rankValues = {
[1] = "F",
[2] = "E",
[3] = "D",
[4] = "C",
[5] = "B",
[6] = "A",
[7] = "S",
[8] = "X"
}
local function createNewObject(typeOfObject, nameOfObject, parentOfObject)
local newObject = Instance.new(typeOfObject)
newObject.Name = nameOfObject
newObject.Parent = parentOfObject
return newObject
end
Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats", 5)
if not leaderstats then
leaderstats = createNewObject("Folder", "leaderstats", player)
end
local fakerank = leaderstats:FindFirstChild("Rank")
if not fakerank then
fakerank = createNewObject("StringValue", "Rank", leaderstats)
end
local pstats = player:WaitForChild("PlrStats", 5)
if not pstats then
pstats = createNewObject("Folder", "PlrStats", player)
end
local RANK = pstats:FindFirstChild("RANK")
if not RANK then
RANK = createNewObject("IntValue", "RANK", pstats)
end
RANK.Changed:Connect(function(newValue)
local tableValue = rankValues[newValue]
if tableValue ~= nil then
fakerank.Value = tableValue
end
end)
end)
If you have any questions about this, feel free to ask
Appreciate this! I do have another script that is responsible for the creation of all of the other values and such. I’ll consider implimenting the table into that script!