Need a Way to Access Leaderstats Name Without Memorizing

What does the code do and what are you not satisfied with?
The following code below allows me to access certain values that are tracked inside my game without requiring me to remember all the stats I have added. It’s not too much of what I am not satisfied with, but I am looking to see from you guys if this code can be taken at a different approach.

What potential improvements have you considered?
I was thinking of reducing it to its according keys without the necessary name… but that would make my overall module script a little unorganized/unlabeled if I decided to take a break from this project.

How (specifically) do you want to improve the code?
As mentioned before, I want another way I can easily access this without the need to type the path to get a single string.

-- Module Script
local module = {
	["Player Settings"] = {
		["Ingame Stat Tracker Name"] = {
			["Assist"] = "Assist",
			["Accuracy"] = "Accuracy",
			["Elimination"] = "Elimination",
			["Death"] = "Death"
		}
	}

-- Server Script
local player = ...
player["Ingame Stat Tracker"][module["Player Settings"]["Ingame Stat Tracker Name"]["Assist"]].Value = 0
-- Equivalent
player["Ingame Stat Tracker"].Assist.Value = 0
3 Likes

The key is to store the information inside game objects. I recommend you to follow this tutorial In-Experience Leaderboards | Documentation - Roblox Creator Hub. Alternatively, you can use things like ‘StringValue’ or ‘IntValue’ without an in-game leaderboard to store stats.

I know how to use stringvalue and intvalue to store things including ingame leaderboard. The answer I want to receive is having some kind of easy access for me to view the name of the stats i have made without memorizing, especially when i have 10+ stat names.

I don’t understand why my answer is incorrect. You can have one named game object per stat. This way, you do not have to store the stats in memory.

local stat = Instance.new("IntValue", game:GetService("Players").LocalPlayer)
stat.Name = "Kills"
stat.Value = 14

print(stat.Name, stat.Value) -- -> Kills, 14

Additionally, you can programmatically keep track of what stats you have.

local statsParent = game:GetService("Players").LocalPlayer

local function createStat(name, type_, initialValue)
    local stat = Instance.new(type_)
    stat.Name = name
    stat.Value = initialValue

    return stat
end

local statValues = {
    createStat("Kills", "IntValue", 0),
    createStat("Deaths", "IntValue", 0)
}

-- For easy access
local statValuesByName = {}
for _, value in pairs(statValues) do
    statValuesByName[value.Name] = value
end

print(statValuesByName["Kills"]) -- -> Kills object

Maybe I didn’t explain it clearly. What I want to do is to have a system in a way I can reference using the Roblox autocomplete system that allows me to know what are the different stats that I have made in-game without the need to remember 10+ different stats. How I see this beneficial in the module script I made above is that I am able to reference the script without having to go back and scroll all the way to the top of the script to see how I labeled my stats, especially when I may have poor memory of the name of the stats I made or if I take a long break from this project.

Generally, I try not to have spaces in the keys for my module scripts, this allows me to just index them with . instead of writing ["key name"]

i.e.

local module = {
    PlayerSettings = {
        IngameStatTrackerName = {
            Assist = "Assist",
            ...
        }
    }
}

-- index later
module.PlayerSettinsg.IngameStatTrackerName.Assist

Although, since you’re just trying to not memorize the names, why not just use this table as a custom type, which would give you linting support without having to type the whole module["Player Settings"]...

local player: Player & typeof(module) = ...
player["Player Settings"] -- should come up as a autocomplete option

1 Like

I have never thought or seen this sort of feature before! I was only used to having something like “player: Player” for my parameters, but never thought it could work on variables as well. I will mark this as a solution in a couple of minutes if no other better answers show up, but this will definitely help organize my scripts better in the long term, or at least support me using the autocomplete system!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.