EXP DATASTORE SCRIPT ROBLOX

95 Views Asked by At

Basically when you gain a level, it goes from whatever level you've just gotten back to zero (level 1 -> level 2 (1 second-ish later) -> level 0) heres the code, its messy ;-;.

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
    
    local exp = Instance.new("StringValue", player)
    exp.Name = "exp"

    local Level = Instance.new("IntValue", exp)
    Level.Name = "Level"
    Level.Value = 1
    local Exp = Instance.new("IntValue", exp)
    Exp.Name = "Exp"
    Exp.Value = 0

    local RequiredExp = Instance.new("IntValue", player)
    RequiredExp.Name = "RequiredExp"
    RequiredExp.Value = Level.Value * 125

    --level up and xp down here

    Exp.Changed:Connect(function(Changed)
        if Exp.Value >= RequiredExp.Value then
            Exp.Value = Exp.Value - RequiredExp.Value

            Level.Value += 1
            RequiredExp.Value = Level.Value * 125
            
            
            local expdata
            local leveldata
            local success, errormessage = pcall(function()
                expdata = myDataStore:GetAsync(player.UserId.."-Exp")
                leveldata = myDataStore:GetAsync(player.UserId.."-Level")
            end)
            
            if success then
                Exp.Value = expdata
                Level.Value = leveldata
            else
                print("error getting your data sorry bruhng")
                warn(errormessage)
            end
        end
    end)
    game.Players.PlayerRemoving:Connect(function(player)
        
        local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId.."-Exp",player.exp.Exp.Value)
        myDataStore:SetAsync(player.UserId.."-Level",player.exp.Level.Value)
            
        end)
        if success then
            print("data saved!")
        else
            print("there was an error in saving data")
            warn (errormessage)
        end
    end)
end)

I have other scripts which are simple that just give xp when you touch a block (for testing), but this is the main script used.

1

There are 1 best solutions below

0
Kylaaa On

Let's step through your code for when the player's experience changes :

Exp.Changed:Connect(function(Changed)
    -- check if they have enough to level up
    if Exp.Value >= RequiredExp.Value then
        -- they do, so...
        -- subtract the required experience from their current exp
        Exp.Value = Exp.Value - RequiredExp.Value

        -- give them the level up, and update the required exp for the next level
        Level.Value += 1
        RequiredExp.Value = Level.Value * 125
            
        -- ???
        local expdata
        local leveldata
        local success, errormessage = pcall(function()
            -- load the previous value stored for the user's experience
            expdata = myDataStore:GetAsync(player.UserId.."-Exp")

            -- load the previous value stored for the user's level
            leveldata = myDataStore:GetAsync(player.UserId.."-Level")
        end)
            
        if success then
            -- overwrite the current experience and level information with the previously saved data
            Exp.Value = expdata
            Level.Value = leveldata
        else
            print("error getting your data sorry bruhng")
            warn(errormessage)
        end
    end
end)

If the fact that the player's level up information is being reverted is a bug, then don't overwrite the current level information with the previously saved data.

Exp.Changed:Connect(function(Changed)
    if Exp.Value >= RequiredExp.Value then
        Exp.Value = Exp.Value - RequiredExp.Value
        Level.Value += 1
        RequiredExp.Value = Level.Value * 125
    end
end)