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

Why is my data store system only temporarily storing my data?

Asked by 4 years ago

I'm new to DataStoreSystems, but I watched this YouTube video on how to make one for my wins system, and even though the points go up, they don't permanently save. When I log back in, my wins just set back to 0. What did I do wrong?


local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player

local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats

local data
local success, errormessage = pcall(function()
    data = myDataStore:GetAsync(player.UserId.."-Wins")
end)

if success then
    wins.Value = data
else
    print("Data not saved from success")
    warn(errormessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() myDataStore:SetAsync(player.UserId.."-Wins",player.leaderstats.Wins.Value) end)

if success then
    print("Data successfully saved")
else
    print("Data not saved")
    warn(errormessage)
end

end)

1 answer

Log in to vote
0
Answered by
iVmk3 -143
4 years ago

This save system prevents hackers and it autosaves.

local PlayerStatManager = {}

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

local sessionData = {}

local AUTOSAVE_INTERVAL = 60

function PlayerStatManager:ChangeStat(player, statName, value)
    local playerUserId = "Player_" .. player.UserId
    assert(typeof(sessionData[playerUserId[statName]) == typeof(value), "ChangeStat error: types do not match"
    if typeof(sessionData[playerUserId][statName]) == "number" then
        sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
    else
        sessionData[playerUserId][statName] = value
    end
end

local function setupPlayerData(player)
    local playerUserId = "Player_" .. player.UserId
    local success, data = pcall(function()
        return playerData:GetAsync(playerUserId)
    end)
    if success then
        if data then
            sessionData[playerUserId] = data
        else
            sessionData[playerUserId] = {Points=0, Experience=0}
        end
    end
    local data = playerData:GetAsync(playerUserId)
    if data then
        sessionData[playerUserId] = data
        else
            sessionData[playerUserId] = {Points=0, Experience=0}
        end
    else
        warn("Cannot access data stor for player!")
    end
end

local function savePlayerData(playerUserId)
    if sessionData[playerUserId] then
        local tries = 0
        local success
        repeat
            tries = tries + 1
            success = pcall(function()
                playerData:SetAsync(playerUserId, sessionData[playerUserId])
            end)
            if not success then wait(1) end
        until tries == 3 or success
        local success, err = pcall(function()
            playerData:SetAsync(playerUserId, sessionData[playerUserId])
        end)
        if not success then
            warn("Cannot save data for player!")
        end
    end
end)

local function saveOnExit(player)
    local playerUserId = "Player_" .. player.UserId
    savePlayerData(playerUserId)
end

local function autoSave()
    while wait(AUTOSAVE_INTERVAL) do
        for playerUserId, data in pairs(sessionData) do
            savePlayerData(playerUserId)
        end
    end
end

spawn(autoSave)

game.Players.PlayerAdded:Connect(setupPlayerData)

game.Players.PlayerRemoving:Connect(saveOnExit)

return PlayerStatManager

Although, This is the standard script from ROBLOX script thing idk what it is called but it's from ROBLOX.

Line 12 is still one big line, the answer bar did not have enough room.

0
I hope this works for you! If it does then please mark this as answered. iVmk3 -143 — 4y
0
How would I add a leaderstats like thing where it displays the value of the wins on the side into this script? NotOriginal12345 9 — 4y
0
For leaderstats you would need a regular script. Also, Did you place this into a ModuleScript? I forgot to tell you that. iVmk3 -143 — 4y
Ad

Answer this question