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

How can I DataStore Tables?

Asked by 5 years ago

I wanna DataStore this table because I'm trying to make an inventory script. Thing is, is that it won't save. Any help?

local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerDataV1");
local itemlist = {}

game.Players.PlayerAdded:Connect(function(Player)
    local inv = Instance.new("Folder",Player)
    inv.Name = "Inventory"
     local ls = Instance.new("IntValue",inv)
        ls.Name = "LinkedSword"
        ls.Value = 6
local Pistol = Instance.new("IntValue",inv)
Pistol.Name = "Pistol"
Pistol.Value = 7
        table.insert(itemlist, ls.Value)
table.insert(itemlist, Pistol.Value)
          Datastore:GetAsync(Player.UserId)

end)


game.Players.PlayerRemoving:Connect(function(Player)
    Datastore:SetAsync(Player.UserId, itemlist)
end)

1 answer

Log in to vote
0
Answered by 5 years ago

Hey Bl_ueHistory

Hope this helps I commented on the code a little so you can see it :)

Please remember to upvote if it works! :D

local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerDataV1");
local itemlist = {}

game.Players.PlayerAdded:Connect(function(player)
    local inv = Instance.new("Folder",player)
    inv.Name = "Inventory"
    if Datastore:GetAsync(player.UserId) then -- If the player has data then load it.
        for i,v in pairs(Datastore:GetAsync(player.UserId)) do -- Loop through the tables in the data
            print("Name: " .. v[1] .. " || Price: " .. v[2])
        end
    else
        local int = Instance.new("IntValue", inv)
        int.Name = "LinkedSword"
        int.Value = 6

        local Pistol = Instance.new("IntValue",inv)
        Pistol.Name = "Pistol"
        Pistol.Value = 7
    end
end)


game.Players.PlayerRemoving:Connect(function(player)
    for i,v in pairs(player.Inventory:GetChildren()) do -- Loop through Inventory folder.
    table.insert(itemlist, {v.Name, v.Value}) -- Insert the Name, Value into a table.
end
Datastore:SetAsync(player.UserId, itemlist) -- save the data.
warn("saved")
end)
Ad

Answer this question