Ad
Log in to vote
0

Saving folder's children with DataStore?

Asked by 9 days ago

I am trying to save a players whole inventory folder with 1 save. For example, when a player joins he will start with a sword, and by the time he leaves he could have multiple swords in his inventory, and it will save on leave.

Code:

local DataStore = game:GetService("DataStoreService"):GetDataStore("Test")
local Players = game:GetService("Players")

local itemlist = {}

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

    local key = "tools_" .. tostring(player.UserId)
    -- inv
    if not player:FindFirstChild("Inventory") then
        local invs = Instance.new("Folder",player)
        invs.Name = "Inventory"
    end
    wait()  
    local inv = player:WaitForChild("Inventory")
    -- starting sword
    if not inv:FindFirstChild("LinkedSword") then
        local ls = Instance.new("IntValue",inv)
        ls.Name = "LinkedSword"
        ls.Value = 1
        table.insert(itemlist, ls.Name)
    end
    DataStore:SetAsync(player.UserId, itemlist)
    wait()
    -- leaderstats
    local leader = Instance.new("Folder",player)
    leader.Name = "leaderstats"
    local cash = Instance.new("IntValue",leader)
    cash.Name = "Cash"
    cash.Value = DataStore:GetAsync(player.UserId) or 0
    cash.Changed:Connect(function()
        DataStore:SetAsync(player.UserId, cash.Value)
    end)
end)


game.Players.PlayerRemoving:connect(function(player)
    wait(2)
    local key = "tools_" .. tostring(player.UserId)
    local inv = player:FindFirstChild("Inventory"):GetChildren()

    DataStore:SetAsync(player.UserId, itemlist)
    DataStore:SetAsync(player.UserId, player.leaderstats.cash.Value)
end)

The leaderstats is a test that does save, but saving the itemlist table breaks it, making it not save at all. Whenever i remove the itemlist table save, the playerstats will save, but otherwise, no. I am trying to make it where if a player gets another weapon in his inventory, it will put it into the table and save it. Any help?

Hi.

Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.

.

..

...

Now back to your regularly scheduled Scripting Helpers....

...

..

.

1 answer

Log in to vote
0
Answered by 9 days ago

This is because you are overriding a key twice when saving. Think about it like delivering mail.

1) You get the key to the whole entire mailbox 2) You put mail in the mailboxes 3) You lock the mailbox

If you save to the same box (or key), you cannot find it because it is buried with the new mail (the key was overwritten). To fix this, you can use tables like so

local data = {
    ['money'] = 500;
    ['rating'] = 10/10;
    ['datastores working'] = true
}

So in your case, you'd do

game.Players.PlayerRemoving:connect(function(player)
    --wait(2) oh my gosh the player is gonna be gone by now
    local key = "tools_" .. tostring(player.UserId)
    local inv = player:FindFirstChild("Inventory"):GetChildren()
    table.insert(itemlist, player.leaderstats.cash.Value)

    DataStore:SetAsync(player.UserId, itemlist)
    DataStore:SetAsync(player.UserId, player.leaderstats.cash.Value)
end)

Also, I recommend keeping the Itemlist in ServerStorage because different players have different items, right?

Another tip: You should use :BindToClose() like so to extend the server length when no one is in da server

game:BindToClose(function()
    warn('D: why did everyone leave dis is not a bad game ')
    wait(30)
end)

Hope this helps!

Ad

Answer this question



Ad