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

data not saving when i play in game?

Asked by 4 years ago

pls help. it prints "data saved" when i test my game with two players on studio, but when i join the game and reset my character, and then join back my death won't save.

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")
wait(5)

game.Players.PlayerAdded:connect(function(player)

    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    local kills = Instance.new("IntValue",leaderstats)
    kills.Name = "Kills"

    local deaths = Instance.new("IntValue",leaderstats)
    deaths.Name = "Deaths"


    local data
    local success, errormsg = pcall(function()
        data = myDataStore:SetAsync(player.UserId,player.leaderstats.Deaths.Value)
    end)

    if success then
        player.leaderstats.Deaths.Value = data
        print("data loaded")
    else
        warn(errormsg)
        print("Data not saved")
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)

    local success, errormsg = pcall(function()
    myDataStore:SetAsync(plr.UserId,plr.leaderstats.Deaths.Value)
    end)

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

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function()
        local humanoid = plr.Character:WaitForChild("Humanoid")
        local deaths = plr.leaderstats.Deaths
        humanoid.Died:connect(function()
            deaths.Value = deaths.Value + 1
        end)
    end)
end)
0
Line 9 should be :GetAsync(player.UserId) instead of :SetAsync(player.UserId), for getting the stats, you don't need to define the value as it's returned as a table. And you can add your characterAdded, death stuff in the first player added under the 2nd to last end. killerbrenden 1537 — 4y
0
works now, thank you! also one more question, how would i save multiple values at once? e.g. saving kills and deaths COMBATKOALA 5 — 4y
0
nvm i read your code i'll try to put it into a table COMBATKOALA 5 — 4y

1 answer

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

Error 1

Line 9 Should be :GetAsync(), instead of :SetAsync()

:GetAsync() is to get the stored data for the player.

:SetAsync() is to set the new values for the player.

These aren't really errors, but just tips.

For getting the stats, you don't need to define the value as it's returned as a table, so you would have to index the table if you have multiple values.

And you can add your characterAdded, death stuff in the first player added under the 2nd to last end

This is how I would do it, even though I saw an error and a few improvements.

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")
wait(5)

game.Players.PlayerAdded:connect(function(player)

    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    local kills = Instance.new("IntValue",leaderstats)
    kills.Name = "Kills"

    local deaths = Instance.new("IntValue",leaderstats)
    deaths.Name = "Deaths"

    local data
    local success, errormsg = pcall(function()
        data = myDataStore:GetAsync(player.UserId)
    end)

    if success then
        player.leaderstats.Deaths.Value = data[1] or 0
        player.leaderstats.Kills.Value = data[2] or 0 --// Edited
        print("Data loaded")
    else
        warn(errormsg)
        print("Data not saved")
    end

    player.CharacterAdded:connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        local deaths = player.leaderstats.Deaths
        humanoid.Died:connect(function()
            deaths.Value = deaths.Value + 1
        end)
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)

    local dataToSave = {
        plr.leaderstats.Deaths.Value,
        plr.leaderstats.Kills.Value --// Edited
    }

    local success, errormsg = pcall(function()
        myDataStore:SetAsync(plr.UserId,dataToSave)
    end)

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

Hope this helped and worked! From my experience, DataStores have been janky very recently.

0
how would I save multiple values? i am struggling on how to use a table to save kills and deaths COMBATKOALA 5 — 4y
0
I edited the table on line 43, and I edited line 23 to load the data. You can check how I did it. killerbrenden 1537 — 4y
Ad

Answer this question