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

data won't save, loads old data.. help?

Asked by 7 years ago

So i'm working on a game with a point system, and it was able to save data just fine.. but later on it just broke randomly and now it won't save points, and always loads old data.. I'm new on databases and such, so anyone know what's wrong with my script?

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("******")
local lvl = DataStore:GetDataStore("******")

game.Players.PlayerAdded:connect(function(player)
 local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"
 local Cash = Instance.new("IntValue",leader)
 Cash.Name = "Cash"
 Cash.Value = ds:GetAsync(player.UserId) or 0
 ds:SetAsync(player.UserId, Cash.Value)
 Cash.Changed:connect(function()
  ds:SetAsync(player.UserId, Cash.Value)
 end)
local Level = Instance.new("IntValue", leader)
 Level.Name = "Lvl"
 Level.Value = lvl:GetAsync(player.UserId) or 0
 lvl:SetAsync(player.UserId, Level.Value)
 Level.Changed:connect(function()
  lvl:SetAsync(player.UserId, Level.Value)
 end)
end)


game.Players.PlayerRemoving:connect(function(player)
 ds:SetAsync(player.UserId, player.leaderstats.Cash.Value)
 lvl:SetAsync(player.UserId, player.leaderstats.Lvl.Value)
end)

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

It looks like your'e using the same DataStore to try to save cash and level:

local ds = DataStore:GetDataStore("******")
local lvl = DataStore:GetDataStore("******")

These will overwrite each other, whichever one you SetAsync() last. A step in the right direction would be to at least use a different DataStorefor each value:

local ds = DataStore:GetDataStore("PlayerCash")
local lvl = DataStore:GetDataStore("PlayerLvls")

A better solution would be to use a single DataStore to save both of these player stats, and save them as a table:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("PlayerStats")

stats = {} -- Table to hold stats of all the players
-- To get only one player's stats, you would use "stats[player]" to get a
--    table containing: { Cash=X, Lvl=Y }

game.Players.PlayerAdded:connect(function(player)
    local leader = Instance.new("Folder",player)
    leader.Name = "leaderstats"

    -- Get any saved stats and put them in our stats table
    stats[player] = ds:GetAsync(player.UserId) or {}
    -- By adding "[player]" we're sure to only access this player's stats

    local Cash = Instance.new("IntValue",leader)
    Cash.Name = "Cash"
    Cash.Value = stats[player].Cash or 0
    Cash.Changed:connect(function()
        stats[player].Cash = Cash.Value -- Update player table's Cash index
        ds:SetAsync(player.UserId, stats[player]) -- Save player table to ds
    end)
    local Level = Instance.new("IntValue", leader)
    Level.Name = "Lvl"
    Level.Value = stats[player].Lvl or 0
    Level.Changed:connect(function()
        stats[player].Lvl = Level.Value -- Update player table's Lvl index
        ds:SetAsync(player.UserId, stats[player]) -- Save player table to ds
    end)
end)

game.Players.PlayerRemoving:connect(function(player)
    if stats[player] then
        ds:SetAsync(player.UserId, stats[player]) -- Save player table to ds
        stats[player] = nil -- Prune our player's stats from our stats table
    end
end)
0
I tried your method, but It does the same thing. I believe it's an issue with filtering enabled, because it works while FE is disabled, but otherwise.. im_Draco 3 — 7y
0
Nevermind, I found my problem and fixed it. Thank you for your help though! im_Draco 3 — 7y
Ad

Answer this question