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

Data Store Problems?

Asked by 9 years ago

This only loads some of the cash that the player had

local DataStoreService = game:GetService("DataStoreService")
local Money = DataStoreService:GetDataStore("Money") -- 

game.Players.PlayerAdded:connect(function(plr)
    local key = "user_"..plr.userId

    local stats = Instance.new("IntValue",plr)
    stats.Name = "leaderstats"
    local level = Instance.new("StringValue",stats)
    level.Name = "Mode"
    level.Value = "Choosing"
    local money = Instance.new("IntValue",stats)
    money.Name = "Cash"     

    money.Value = Money:GetAsync(key) or 0 
    money.Changed:connect(function() Money:SetAsync(key, money.Value) end) 
end)

1 answer

Log in to vote
2
Answered by
LuaQuest 450 Moderation Voter
9 years ago

Try using UpdateAsync, and making a function to determine whether or not an old key was saved. Like this:

local DataStoreService = game:GetService'DataStoreService'
local Money = DataStoreService:GetDataStore'Money'
local Players = game:GetService'Players'

local DefaultMoney = 1 -- what they start with

local datalib = {} -- library for data functions

datalib.SaveData = function(store, key, value)
    if store:GetAsync(key) then -- if old, than update
        store:UpdateAsync(key, function()
            return value or DefaultMoney  -- update with this new value (or default)
        end)
    else
        store:SetAsync(key, value) -- if not, make a new store
    end
end

Players.PlayerAdded:connect(function(plr)
    local key = "user_"..plr.userId

    local stats = Instance.new("IntValue",plr)
    stats.Name = "leaderstats"
    local level = Instance.new("StringValue",stats)
    level.Name = "Mode"
    level.Value = "Choosing"
    local money = Instance.new("IntValue",stats)
    money.Name = "Cash"
    money.Value = Money:GetAsync(key) or DefaultMoney 

    money.Changed:connect(function()
        datalib.SaveData(Money, key, money.Value)
    end)
end)
0
This makes the value 0 every time someone joins. supermanswaqq 65 — 9y
1
k lemme test it LuaQuest 450 — 9y
1
oh, i know why. give me a sec. LuaQuest 450 — 9y
Ad

Answer this question