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

Can't Save more than one value?

Asked by 7 years ago

pls help! everytime i try to add more values the script ends up glitchy and unresponsive. If u know why this is happening pls tell me.


local DataStore = game:GetService("DataStoreService") local Ds1 = DataStore:GetDataStore("DS") game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("IntValue",player) stats.Name = "leaderstat" local gold = Instance.new("IntValue",stats) gold.Name = "Gold" gold.Value = Ds1:GetAsync(player.UserId) or 100 Ds1:GetAsync(player.UserId, gold.Value) end) game.Players.PlayerRemoving:connect(function(player) print("Saving Data") Ds1:SetAsync(player.UserId, player.leaderstat.Gold.Value) print(player.Name.."'s Data Saved") end)
1
use a table to save and load all the values at once. LostPast 253 — 7y

1 answer

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

You need to put them in a table in order to save multiple values.

local DataStore = game:GetService("DataStoreService")
local Ds1 = DataStore:GetDataStore("DS")

game.Players.PlayerAdded:connect(function(player)
    local stats = Instance.new("IntValue",player)
    stats.Name = "leaderstat"

    local data = Ds1:GetAsync(player.UserId) --// Get the data.
    local gold = Instance.new("IntValue",stats)
    gold.Name = "Gold"
    gold.Value = data[1] or 100 --// And then just set the gold value to the first item in the table(or 100, if it doesn't exist).
end)

game.Players.PlayerRemoving:connect(function(player)
    print("Saving Data")
    Ds1:SetAsync(player.UserId, {
        player.leaderstat.Gold.Value, --// First position,
        someothervalue.Value, --// Second position,
        yetanothervalue.Value, --// Third, & onward.
    })
    print(player.Name.."'s Data Saved")
end)

Hope this helped.

0
I'm having problems with this script. Every Time i run it the gold value dosen't show. tailsninja99 25 — 7y
Ad

Answer this question