#Leveling system

38 messages · Page 1 of 1 (latest)

stray pulsar
#

I've attempted adding some checks, saving data isn't the issue. Just adding.

#

if anyone can fix this, im willing to compensate

#

like 200 robux or so

#

not much but im desperate for a solution

#

my script keeps breaking every so often

stray pulsar
#

Part one of script

local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("Game")

local experienceMultiplier = 1.05
local baseExperienceThreshold = 100

local exponent = 1.8
local mult = 100
local cachedLevels = {}

local function expFormula(level: number)
    if cachedLevels[level] then
        return cachedLevels[level]
    end

    local exp = level ^ exponent
    exp = exp * mult
    exp = math.floor(exp / 5) * 5

    cachedLevels[level] = exp

    return exp
end

local function calculateExperienceThreshold(level)
    return baseExperienceThreshold * (experienceMultiplier ^ (level - 1))
end

local function handleLevelUp(plr, experience, level)
    while true do
        local requiredExperience = calculateExperienceThreshold(level.Value)
        if experience.Value >= requiredExperience then
            level.Value = level.Value + 1
            experience.Value = experience.Value - requiredExperience
            print(plr.Name .. " leveled up to level " .. level.Value .. "!")
        else
            break
        end
    end
end

local function loadPlayerData(plr)
    local leaderstats = plr:FindFirstChild("leaderstats")
    if not leaderstats then
        warn(plr.Name .. ": Leaderstats not found.")
        return
    end

    local Level = leaderstats:FindFirstChild("Level")
    local Experience = leaderstats:FindFirstChild("Experience")
    local Money = leaderstats:FindFirstChild("Money") -- Get the Money value

    if not Level or not Experience or not Money then
        warn(plr.Name .. ": Level, Experience, or Money IntValue not found.")
        return
    end
#

Part 2

    local plrKey = "id_" .. plr.UserId
    local success, errorMessage = pcall(function()
        local savedData = ds:GetAsync(plrKey)
        if savedData then
            if savedData.Level then
                Level.Value = savedData.Level
                print("Loaded Level:", savedData.Level)
            end
            if savedData.Experience then
                Experience.Value = savedData.Experience
                print("Loaded Experience:", savedData.Experience)
            end
            if savedData.Money then -- Load Money
                Money.Value = savedData.Money
                print("Loaded Money:", savedData.Money)
            end
        else
            print("No saved data found for player " .. plr.Name .. ". Using default values.")
        end
    end)

    if not success then
        warn("Failed to load data for player " .. plr.Name .. ": " .. errorMessage)
    end
end

local function savePlayerData(plr)
    local leaderstats = plr:FindFirstChild("leaderstats")
    if not leaderstats then
        warn(plr.Name .. ": Leaderstats not found.")
        return
    end

    local Level = leaderstats:FindFirstChild("Level")
    local Experience = leaderstats:FindFirstChild("Experience")
    local Money = leaderstats:FindFirstChild("Money") -- Get the Money value

    if not Level or not Experience or not Money then
        warn(plr.Name .. ": Level, Experience, or Money IntValue not found.")
        return
    end

    local plrKey = "id_" .. plr.UserId
    local success, errorMessage = pcall(function()
        ds:SetAsync(plrKey, {
            Level = Level.Value,
            Experience = Experience.Value,
            Money = Money.Value, -- Save Money
        })
        print("Saved Level:", Level.Value)
        print("Saved Experience:", Experience.Value)
        print("Saved Money:", Money.Value)
    end)

    if not success then
        warn("Failed to save data for player " .. plr.Name .. ": " .. errorMessage)
    end
end

game.Players.PlayerAdded:Connect(function(plr)
    wait()

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local Level = Instance.new("IntValue")
    Level.Name = "Level"
    Level.Value = 1
    Level.Parent = leaderstats    
    
    local Money = Instance.new("IntValue")
    Money.Name = "Money"
    Money.Value = 0
    Money.Parent = leaderstats
    


    local Experience = Instance.new("IntValue")
    Experience.Name = "Experience"
    Experience.Value = 0
    Experience.Parent = leaderstats



    loadPlayerData(plr)
    handleLevelUp(plr, Experience, Level)

    Experience.Changed:Connect(function()
        handleLevelUp(plr, Experience, Level)
    end)

    while plr and plr.Parent do
        wait(1)
        handleLevelUp(plr, Experience, Level)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    savePlayerData(plr)
end)

local function addExperience(plr, amount)
    if plr and plr.leaderstats then
        local experience = plr.leaderstats:FindFirstChild("Experience")
        if experience then
            experience.Value = experience.Value + amount
            handleLevelUp(plr, experience, plr.leaderstats.Level)
        end
    end
end

return {
    addExperience = addExperience,
    expFormula = expFormula,
}
umbral grail
#

how are you updating the players experience?

#

are you sending exp the player gains on the client side?

warm cairnBOT
#

studio** You are now Level 3! **studio

stray pulsar
#

wait no

#

mostly server

#

though there is another script which gives the exp + other stuff from beating a wave

umbral grail
#

if its client sided i would recommend using a remote event to send an update to server based on the amount of exp you give the player

#

your script does work for me server sided changing the exp amount but it will not update if you are only sending the exp to the client

stray pulsar
#

its all server

#

i checked

#

all server

#

another script does the addition parts

#

lemmi get it

#
        local MarketplaceService = game:GetService("MarketplaceService")
        local MoneygamepassId = 1077855402
        local ExpgamepassId = 1079201768

        for _, player in pairs(game.Players:GetPlayers()) do
            if currentWave > 1 then
                if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Money") and player.leaderstats:FindFirstChild("Experience") then
                    local moneyToAdd = 300 + (10 * currentWave)
                    local expToAdd = 30 + (10 * currentWave)
                    if MarketplaceService:UserOwnsGamePassAsync(player.UserId, MoneygamepassId) then
                        moneyToAdd = 600 + (10 * currentWave)
                    end
                    if MarketplaceService:UserOwnsGamePassAsync(player.UserId, ExpgamepassId) then
                        expToAdd = 60 + (10 * currentWave)
                    end
                    player.leaderstats.Money.Value += moneyToAdd
                    player.leaderstats.Experience.Value += expToAdd
                end
            end
        end
#

this is all there is to adding

#

its all server

#
local function handleLevelUp(plr, experience, level)
    while true do
        local requiredExperience = calculateExperienceThreshold(level.Value)
        if experience.Value >= requiredExperience then
            level.Value = level.Value + 1
            experience.Value = experience.Value - requiredExperience
            print(plr.Name .. " leveled up to level " .. level.Value .. "!")
        else
            break
        end
    end
end
#

this handles leveling up

#

so i think its something with this

#

the script works sometimes

#

like if u reset data

#

it works for a while

#

but if you rejoin, it just overshoots the experience threshold

#

ts what i mean

umbral grail
#

hm ill try using profile service to save the data if you'd like me to but it may take a me a minute to get your script to work with it if you'd like me to see if that might fix it

stray pulsar
#

sure, im literally looking for anything 🙏

#

anything which works lol

umbral grail
#

ill dm you whenever i finish it up!

stray pulsar
#

epic thanks!

#

hero