Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to save these NumberValues?

Asked by 2 years ago

Hello. I am wondering how to save these NumberValues. It is for a level up and exp system. With the way I have tried, the bar breaks and it makes Level and Exp the same value. Please help!

This first script is a 'leaderstats' script in ServerScriptService

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(Player)

    -- Leaderstats --

    local leaderstats = Instance.new("Folder", Player)
    leaderstats.Name = "leaderstats"

    local Level = Instance.new("NumberValue", leaderstats)
    Level.Name = "Level"
    Level.Value = 1

    local Exp = Instance.new("NumberValue", leaderstats)
    Exp.Name = "Exp"
    Exp.Value = 0

    local RequiredExp = Instance.new("NumberValue", Player)
    RequiredExp.Name = "RequiredExp"
    RequiredExp.Value = Level.Value * 1000

    local playerUserId = "Player_"..Player.UserId
    local data = playerData:GetAsync(playerUserId)

    if data then
        Level.Value = data
        Exp.Value = data
    else
        Level.Value = 1
        Exp.Value = 1
    end

    --Level Up & Exp--

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

            Level.Value += 1
            RequiredExp.Value = Level.Value * 1000
        end
    end)    
end)

local function onPlayerExit(Player)
    local success, err = pcall(function()
        local playerUserId = "Player_"..Player.UserId
        playerData:SetAsync(playerUserId, Player.leaderstats.Exp.Value)
        playerData:SetAsync(playerUserId, Player.leaderstats.Level.Value)
    end)
    if not success then
        warn("Could not save data")
    end
end

game.Players.PlayerRemoving:Connect(onPlayerExit)

This second script is in 'BackgroundFrame'.

local Player = game.Players.LocalPlayer

local Exp = Player.leaderstats.Exp
local RequiredExp = Player.RequiredExp

Exp.Changed:Connect(function(Changed)
    if Changed then
        script.Parent.ExpBar:TweenSize(UDim2.new(Exp.Value / RequiredExp.Value, 0, 1, 0))
    end
end)

while wait() do
    script.Parent.ExpLabel.Text = Exp.Value.." / "..RequiredExp.Value
end

The final script is in 'LevelLabel'.

local Player = game.Players.LocalPlayer

while wait() do
    script.Parent.Text = "Level : "..Player.leaderstats.Level.Value
end

Level Label displays the level. Example = Level : 1

Exp Label displays how much Exp you have. Example = 10 / 1000

Exp Bar is a bar that goes up depending on how much exp you have. For example, if you have 500 / 1000 exp the bar would be at half.

0
you are saving 1 thing and loading 1 thing, setting both the exp and level to 1 thing. save the data in a table. greatneil80 2647 — 2y
0
and how do i do that xbantixx 10 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago

hiya!

your issue here (described by greatneil80) is when you're saving the data and when you're setting the NumberValue's values

this can be seen on lines 27, 28, 49 and 50

greatneil's solution is to impliment this data into a table: a table (also known as an array or a dictionary) can store multiple values of any type except nil values tables are constructed as follows:

-- Construct an empty table assigned to variable "t"
local t = {}
print(t) -- {}

what we need to do, is create a dictionary using a table (defining a specific key and value) and defining a player's experience and level

local UserData = {
    Exp = 0,
    Level = 0
}
print(UserData) -- {Exp = 0, Level = 0}

we can access this data by calling a key on the table

local UserData = {
    Exp = 0,
    Level = 0
}
print(UserData.Exp) -- 0

using this, and your code above: we can merge these two together to make your game work

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(Player)

    -- Leaderstats --

    local leaderstats = Instance.new("Folder", Player)
    leaderstats.Name = "leaderstats"

    local Level = Instance.new("NumberValue", leaderstats)
    Level.Name = "Level"
    Level.Value = 1

    local Exp = Instance.new("NumberValue", leaderstats)
    Exp.Name = "Exp"
    Exp.Value = 0

    local RequiredExp = Instance.new("NumberValue", Player)
    RequiredExp.Name = "RequiredExp"
    RequiredExp.Value = Level.Value * 1000

    local playerUserId = "Player_"..Player.UserId
    local data = playerData:GetAsync(playerUserId)

    if data and type(data) == "table" then
    -- changes here!
        Level.Value = data.Level
        Exp.Value = data.Exp
    else
        Level.Value = 1
        Exp.Value = 1
    end

    --Level Up & Exp--

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

            Level.Value += 1
            RequiredExp.Value = Level.Value * 1000
        end
    end)    
end)

local function onPlayerExit(Player)
    local success, err = pcall(function()
    -- and more changes over here!
        local playerUserId = "Player_"..Player.UserId
        local playerData = {
                Exp = Player.leaderstats.Exp.Value,
                Level = Player.leaderstats.Level.Value
        }
        playerData:SetAsync(playerUserId, playerData )
    end)
    if not success then
        warn("Could not save data")
    end
end

game.Players.PlayerRemoving:Connect(onPlayerExit)

hopefully this answers your question, if it doesn't; feel free to leave a comment and i'll elaborate.

have a good day (and if you celeberate), a happy christmas :)

0
do I need to put the table in a new script or in the same leaderstats script? xbantixx 10 — 2y
0
you can keep the table in the leaderstats script, shown above loowa_yawn 383 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

The script didn't seem to work. I didn't get any errors in output.

Also sorry for late reply!

Answer this question