Help Optimizing My Code

Hey!
I just recently found time to get back into coding, so I wanted to get a bit of a refresher and try and learn a few new things. I know one thing I used to struggle with before was Dictionaries. For some reason, I just never understood them. I took time to look over the wiki and learned quite a bit about them.

This is just a small leveling system that I made. My question is, what is the most optimized way to go about this kind of system? The system I’m trying to create basically has pre-set ‘XPNeeded’ Values, along with the Values for the levels. Below, you can see my attempt at creating it. The code does exactly what I need it to do, but I genuinely believe this isn’t close to what an optimized system would look like.

What would be the best way to go about this? Do I have the right idea of comparing 2 separate dictionaries in an ‘If-Statement’? If not, how should I go about this? Thanks in advance!

local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer

local leaderstats = plr:WaitForChild("leaderstats")
local currentXP = leaderstats:WaitForChild("Experience")
local currentLevel = leaderstats:WaitForChild("Level")

local plrLevels = {
	["Level 1"] = 1,
	["Level 2"] = 2,
	["Level 3"] = 3,
}

local XPNeededPerLevel = {
	["Level 1"] = 100,
	["Level 2"] = 500,
	["Level 3"] = 1000,
}


currentXP.Changed:Connect(function()
	print("Experience has increased to "..currentXP.Value)
	
	for level, xpNeeded in pairs(XPNeededPerLevel) do
		if currentXP.Value == xpNeeded then
			for lvlString, levels in pairs(plrLevels) do
				if lvlString == level then
					currentLevel.Value = levels
					print(plr.Name.." has reached level "..currentLevel.Value)
					break
				end
			end
		end
	end
end)

Honestly, if you are still working on understanding dictionaries (and possibly other data types) I would just not focus on optimization and try and tackle just different problems.

Leveling systems are generally tied to other aspects of the game right? Maybe try and make it so the levels save, etc, make it a module so any other script requiring it can easily get the player level, make it so you can easily increase EXP via that module yk - I would focus on making ur current system more usable for more features. Optimization really isn’t that important, honestly optimization is an endless cycle and u just burn time trying to make code perfect, if u wanna make a good game ur code is most likely going to be mediocre (unless u have a whole team w/ u)

but

for ur current system I personally would have the XP revolve around a module script, so u have ur module script and u could do stuff like :incrementXP(val) :getLevel(), idk tbh
then maybe currentXP.Value >= xpNeeded (what if u increment it too much)

good job tho

1 Like

Don’t make tables of your levels, you only need an XPToLevel function or something similar.

The most common formula for that is:
XP = (level/x)^y , with x affecting the amount of XP (lower values = more XP required per level), and y being how quickly the required xp per level should increase (higher values = larger gaps between levels). (Taken from here.)

Example:

function XPToLevel(XP: number)
	return math.floor(math.sqrt(XP) * 0.07)
end
1 Like

good job ngl,I get what you’re trying to do here. You got the right idea but there’s a more efficient way to manage this. Instead of using two dictionaries and comparing them, you can simplify the whole thing by using one dictionary to map XP needed directly to levels.

Here’s a revamped version of your code:

local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer

local leaderstats = plr:WaitForChild("leaderstats")
local currentXP = leaderstats:WaitForChild("Experience")
local currentLevel = leaderstats:WaitForChild("Level")

local XPNeededPerLevel = {
    [100] = 1,
    [500] = 2,
    [1000] = 3,
}

currentXP.Changed:Connect(function()
    print("Experience has increased to " .. currentXP.Value)
    
    local newLevel = currentLevel.Value
    for xpNeeded, level in pairs(XPNeededPerLevel) do
        if currentXP.Value >= xpNeeded and level > newLevel then
            newLevel = level
        end
    end
    
    if newLevel ~= currentLevel.Value then
        currentLevel.Value = newLevel
        print(plr.Name .. " has reached level " .. currentLevel.Value)
    end
end)

1 Like