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

datastore not working on mutiple items?

Asked by 4 years ago

So in my game you collect stars but I need to save what stars you collected specifically. I have no idea why it doesn't work, sometimes the first star is marked as collected when the game is launched but when you collect more stars it doesn't work at all. When you die you also lose them. here's the code:

local datastore = game:GetService("DataStoreService")
local ds = datastore:GetDataStore("StarSaveSystem")
local dsc = datastore:GetDataStore("CoinSaveSystem")

local star_ds = datastore:GetDataStore("starsSaveSystem")

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    if game.Players:FindFirstChild(player.Name) then
        ----create leaderstats
        local leaderboard = Instance.new("IntValue",game.Players)
        leaderboard.Parent = (game.Players[player.Name])
        leaderboard.Name = "leaderstats"

        --stars
        local stars = Instance.new("IntValue",leaderboard)
        stars.Name = "Stars"

        stars.Value = ds:GetAsync(player.UserId) or 0
        ds:SetAsync(player.UserId, stars.Value)

        --coins
        local coins = Instance.new("IntValue",leaderboard)
        coins.Name = "Coins"

        coins.Value = dsc:GetAsync(player.UserId) or 0
        dsc:SetAsync(player.UserId, coins.Value)

        --update save
        stars.Changed:connect(function()
            ds:SetAsync(player.UserId, stars.Value)
        end)

        coins.Changed:connect(function()
            dsc:SetAsync(player.UserId, coins.Value)
        end)

        ----create player variables
        local HP = Instance.new("IntValue",game.Players)
        HP.Parent = (game.Players[player.Name])
        HP.Name = "HP"
        HP.Value = 3

        --load all collected stars
        local star_load = star_ds:GetAsync(player.UserId)
        if star_load then
            local sd = player.PlayerGui.Stars
            sd.star1.Value = star_load[1]
            sd.star2.Value = star_load[2]
            sd.star3.Value = star_load[3]
            sd.star4.Value = star_load[4]
            sd.star5.Value = star_load[5]
            sd.star6.Value = star_load[6]
        end
        local sd = player.PlayerGui.Stars
        local savetable = {
                sd.star1.Value,
                sd.star2.Value,
                sd.star3.Value,
                sd.star4.Value,
                sd.star5.Value,
                sd.star6.Value
        }
        star_ds:SetAsync(player.UserId, savetable)
        savetable.Changed:connect(function()
            star_ds:SetAsync(player.UserId, savetable)
        end)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local sd = player.PlayerGui.Stars
    local savetable = {
            player.PlayerGui.Stars.star1.Value,
            player.PlayerGui.Stars.star2.Value,
            player.PlayerGui.Stars.star3.Value,
            player.PlayerGui.Stars.star4.Value,
            player.PlayerGui.Stars.star5.Value,
            player.PlayerGui.Stars.star6.Value
    }
    star_ds:SetAsync(player.UserId, savetable)
end)

If you know how to fix it or even a better way to do it, please help me :)

1 answer

Log in to vote
0
Answered by 4 years ago

You cannot save a table, since if you call its variable without giving it an index, it will give you the address of the table

So what i suggest is to convert the table into a string when you gonna save it, then turn the string into a table when you gonna load it.

The easiest way to do this is to use JSONencode and JSONdecode

https://developer.roblox.com/en-us/api-reference/class/HttpService

0
It still doesn't work for some reason Mathisseroblox 2 — 4y
0
Ok I figured it out with your comment. Thanks :) Mathisseroblox 2 — 4y
Ad

Answer this question