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

How can I save/load this particular IntValue?

Asked by 3 years ago
Edited 3 years ago

So I've recently started working on a simple game and as a beginning script writer, I can't get one of my values to save/load properly. I have 4 IntValues that I'm loading and saving, Level, CurrentExp, MaxExp, and Bending. Each number in the Bending IntValue corresponds to a bending from the Avatar series. For some reason, the player's bending value never saves/loads correctly but the others work just fine. I suspect I made a minor error that messes things up, but I've look for a long time now and I can't find it.

-- Storage Definitions
local DS = game:GetService("DataStoreService")
local LevelStore = DS:GetOrderedDataStore("level")
local ExpStore = DS:GetOrderedDataStore("CurrentExp")
local MaxExpStore = DS:GetOrderedDataStore("MaxExp")
local BendingStore = DS:GetOrderedDataStore("Bending")

-- Upon Joining
game.Players.PlayerAdded:Connect(function(player)
    local Level = Instance.new("IntValue", player)
    Level.Name = "Level"
    Level.Value = 1

    local CurrentExp = Instance.new("IntValue", Level)
    CurrentExp.Name = "CurrentExp"

    local MaxExp = Instance.new("IntValue", Level)
    MaxExp.Name = "MaxExp"
    MaxExp.Value = 100

    local Bending = Instance.new("IntValue", player) -- -1 = NonBender, 0 = Null, 1 = Fire, 2 = Air, 3 = Water, 4 = Earth
    Bending.Name = "Bending"

    pcall(function()
        local LvlVal = LevelStore:GetAsync(player.UserId)
        if LvlVal then
            Level.Value = LvlVal
        else
            Level.Value = 1
        end
        local ExpVal = ExpStore:GetAsync(player.UserId)
        if ExpVal then
            CurrentExp.Value = ExpVal
        else
            CurrentExp.Value = 0
        end
        local MaxExpVal = MaxExpStore:GetAsync(player.UserId)
        if MaxExpVal then
            MaxExp.Value = MaxExpVal
        else
            MaxExp.Value = 100
        end
        local BendingVal = BendingStore:GetAsync(player.UserId)
        if BendingVal then
            Bending.Value = BendingVal
        else
            Bending.Value = 0
        end
    end)

    -- Leveling Up
CurrentExp.Changed:Connect(function()
        if CurrentExp.Value >= MaxExp.Value then
            repeat
                Level.Value = Level.Value + 1
                CurrentExp.Value = CurrentExp.Value - MaxExp.Value
                MaxExp.Value = MaxExp.Value * 1.25
            until CurrentExp.Value < MaxExp.Value
        end
    end)

end)

-- Data Saving
game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()

        if player:FindFirstChild("Level") and player.Level.Value > 1 then
            LevelStore:SetAsync(player.UserId, player.Level.Value)
        end
        if player.Level:FindFirstChild("CurrentExp") and player.Level.CurrentExp.Value > 0 then
            ExpStore:SetAsync(player.UserId, player.Level.CurrentExp.Value)
        end
        if player.Level:FindFirstChild("MaxExp") and player.Level.MaxExp.Value > 100 then
            MaxExpStore:SetAsync(player.UserId, player.Level.MaxExp.Value)
        end
        if player:FindFirstChild("Bending") and player.Bending.Value ~= 0 then
            BendingStore:SetAsync(player.UserId, player.Bending.Value)
        end
    end)
end)

Originally, Bending was a StringValue, but I couldn't get it to save/load, so I just made it an IntValue to keep it simple, but if I can make it a string Value again that would be nice. I put code to make a part spawn whenever a players bending is saved by leaving the game (line 78), and the part doesn't spawn. I'm not sure what I did wrong, but if you see it, that would be nice. If you need any more information just ask.

0
Try rewriting it? ScottVulpes 105 — 3y
0
I rewrote the code and corrected a few spelling errors, but I still can't find the main issue. lemark23 7 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

I figured out that the problem for the bending value, unlike the other IntValues, was that I was trying to change them with a LocalScript. I set up a few RemoteEvents and everything works as intended now. Sorry for posting an unsolvable problem, but the problem has been solved.

Ad

Answer this question