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

DataStore not working?[unsolved]

Asked by 8 years ago
datastore = game:GetService("DataStoreService"):GetDataStore("FarmStatsCJ")

local function set(key,value)
        datastore:SetAsync(key,value)
end

local function get(key)
    datastore:GetAsync(key)
end

game.Players.PlayerRemoving:connect(function(player)
    wait(1)
    local l = player.leaderstats
    set('leaderstatsFarmCJ1'..player.userId,l.Money)
    set('leaderstatsFarmCJ2'..player.userId,l.level)
    set('leaderstatsFarmCJ3'..player.userId,l.Harvests)
    set('valsFarmCJ1'..player.userId,player.storage.ex.Value)
    set('valsFarmCJ2'..player.userId,player.storage.exm.Value)
    set('valsFarmCJ3'..player.userId,player.storage.waittime.Value)
    set('valsFarmCJ4'..player.userId,player.storage.code.Value)
    set('inventoryFarmCJ1'..player.userId,player.inventory.Carrots.Value)
    set('inventoryFarmCJ2'..player.userId,player.inventory.Wheat.Value)
    set('inventoryFarmCJ3'..player.userId,player.inventory.Corn.Value)
end)

game.Players.PlayerAdded:connect(function(play)
    wait(2)
    local l = play.leaderstats
    local i = play.inventory
    local s = play.storage
    if l.Money then
        l.Money.Value = get('leaderstatsFarmCJ1'..play.userId)
        l.level.Value = get('leaderstatsFarmCJ2'..play.userId)
        l.Harvests.Value = get('leaderstatsFarmCJ3'..play.userId)
        s.ex.Value = get('valsFarmCJ1'..play.userId)
        s.exm.Value = get('valsFarmCJ2'..play.userId)
        s.waittime.Value = get('valsFarmCJ3'..play.userId)
        i.Carrots.Value = get('inventoryFarmCJ1'..play.userId)
        i.Wheat.Value = get('inventoryFarmCJ2'..play.userId)
        i.Corn.Value = get('inventoryFarmCJ3'..play.userId)
    end
end)

The datastore doesn't work, and since I've never used datastore before, I don't know what i did wrong.

1 answer

Log in to vote
1
Answered by
einsteinK 145
8 years ago
  1. What if the player doesn't have saved data yet? Doing get('idk') would return nil. Doing Money.Value = nil isn't very good.

  2. Why don't you make the leaderstats/inv/... in that script too? Then you don't have to wait.

  3. Why do you wait(1) on PlayerRemoving? The server might shutdown before you can save.

  4. Instead of saving every stat in a separate key, why not put it all in one table?

-- Saving
local stats = {}
stats.Money = Money.Value
stats.Role = Role.Value
set("Data_"..play.userId,stats)
-- Loading
local stats = get("Data_"..play.userId)
if not stats then return end -- NO STATS, USE DEFAULT
Money.Value = stats.Money
Role.Value = stats.Role
0
thank you. the consept of what this is helped me ScriptsAhoy 202 — 8y
Ad

Answer this question