Ad
Log in to vote
0

DataStore service not saving?

Asked by 4 days ago

Hello I'm learning how to make data stores and made one. However it is giving me issues that aren't displayed in the output so I don't know what is causing the issue.

I have a leader board with 2 stats one is an int value and the other is a string value. I have a part that gives me points to the int value and I want this data store to save the newly added points when I leave the game so that if I returned to the game the points will still be there. However when I tested it, the points are not saved and displayed as 0 (Original amount).

This is my script.


local DataStore = game:GetService("DataStoreService"):GetDataStore("Strength") game.Players.PlayerAdded:Connect(function (player) local stats = Instance.new("IntValue", player) stats.Name = "leaderstats" local SpiritClass = Instance.new("StringValue", stats) SpiritClass.Name = "SpiritClass" SpiritClass.Value = "E" local Spirit = Instance.new("IntValue", stats) Spirit.Name = "Spirit" local key = "Player_"..player.UserId local savedvalues = DataStore:GetAsync(key) if savedvalues then -- if the player has played before then it'll load there stats -- format of saving is {SpiritClass, SpiritStrength} SpiritClass.Value = savedvalues[1] Spirit = savedvalues[2] else local valuesToSave = {SpiritClass.Value, Spirit.Value} DataStore:SetAsync(key, valuesToSave) end end) game.Players.PlayerRemoving:Connect(function (player) local key = "Player_"..player.UserId --save key: {Spiritclass, Spirit} local valuesToSave = {player.leaderstats.SpiritClass.Value, player.leaderstats.Spirit.Value} DataStore:SetAsync(key, valuesToSave) end)

1 answer

Log in to vote
-2
Answered by
HaveASip 329 Moderation Voter
4 days ago
Edited 4 days ago

All your script is right, but you have two errors. Both errors about saving data. If you want to save multiple data you need to save it like this

game.Player.PlayerRemoving:Connect(function(player)
    local key = "Player_"..player.UserId

    DataStore:SetAsync(key,{
        player.leaderstats.SpiritClass.Value; -- This is a first one
        player.leaderstats.Spirit.Value; --Second
    })
    --and if you saving multiple data you need to put ";" this before typing next
    --that means you have another info
end

-- and on line 24 put "Value" word
Spirit.Value = savedvalues[2]
0
So I say valuesToSave = {player.leaderstats.SpiritClass.Value; player.leaderstats.Spirit.Value;} ? veileno 7 — 4d
0
Yes but only thing that you need to make info like a list HaveASip 329 — 4d
0
Thank you! veileno 7 — 4d
0
The way this is being saved is correct. Both ; and ,  are valid for a list. kingdom5 4135 — 4d
0
The error was not using.Value on line 24 kingdom5 4135 — 4d
Ad

Answer this question