i want to store a lot of players data into datastore
but i dont know how to use tables to store them
i want to store all of the players data into 1 datastore (inventory, settings, coins)
local players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")
local ok = workspace.h:FindFirstChild("Lobbies")
local dss = game:GetService("DataStoreService")
local sds = dss:GetDataStore("Settings")
local ids = dss:GetDataStore("Inventory")
local cds = dss:GetDataStore("Coins")
local scds = dss:GetDataStore("SaveCoins")
local slotds = dss:GetDataStore("Slots")
local function savesettings(plr)
local settingsFolder = plr.Settings
local settingss = {}
for _, setting in next, settingsFolder:GetChildren() do
settingss[setting.Name] = setting.Value
end
return settingss
end
local function loadsettings(plr, settings)
local settingsFolder = plr.Settings
for _, setting in pairs(settingsFolder:GetChildren()) do
if settings[setting.Name] ~= nil then
setting.Value = settings[setting.Name]
end
end
end
local function loadinv(plr, inv)
local invFolder = plr.Inventory
for _, item in pairs(invFolder:GetChildren()) do
if inv[item.Name] ~= nil then
item.Value = inv[item.Name]
end
end
end
local function saveinv(plr)
local invFolder = plr.Inventory
local invss = {}
for _, item in next, invFolder:GetChildren() do
invss[item.Name] = item.Value
end
return invss
end
local function saveslots(plr)
local invFolder = plr.Slots
local slotss = {}
for _, item in next, invFolder:GetChildren() do
if item.Value ~= nil then
slotss[item.Name] = item.Value.Name
end
end
return slotss
end
local function loadslots(plr, inv)
local invFolder = plr.Slots
for _, item in pairs(invFolder:GetChildren()) do
if inv[item.Name] ~= nil then
item.Value = plr.Inventory:FindFirstChild(inv[item.Name])
print("got " ..inv[item.Name].. " for " ..item.Name)
else
item.Value = nil
end
end
end
players.PlayerAdded:Connect(function(player)
warn("hi " ..player.Name)
local val = Instance.new("ObjectValue")
val.Name = "Player"
val.Value = player
val.Parent = workspace.h.What
local plrcoins = game:GetService("ServerStorage").ChimCoins:Clone()
plrcoins.Parent = player
local plrInv = game:GetService("ServerStorage").InventoryPreset:Clone()
plrInv.Name = "Inventory"
plrInv.Parent = player
local plrslots = game:GetService("ServerStorage").Slots:Clone()
plrslots.Parent = player
plrslots.Slot1.Value = plrInv.Slingshot
plrslots.Slot2.Value = plrInv.Cake
local plrSettings = game:GetService("ServerStorage").SettingsPreset:Clone()
plrSettings.Name = "Settings"
plrSettings.Parent = player
local plrscoins = game:GetService("ServerStorage").SaveCoins:Clone()
plrscoins.Parent = player
local data
local inv
local coins
local slots
local scoins
local success, errorMessage = pcall(function()
data = sds:GetAsync(player.UserId)
inv = ids:GetAsync(player.UserId)
coins = cds:GetAsync(player.UserId)
slots = slotds:GetAsync(player.UserId)
scoins = scds:GetAsync(player.UserId)
end)
if success then
if data then
local setting = data.settings
loadsettings(player, setting)
end
if inv then
local invs = inv.inv
loadinv(player, invs)
end
if coins then
plrcoins.Value = coins
end
if slots then
local slotss = slots.slots
loadslots(player, slotss)
end
if scoins then
plrscoins.Value = scoins
end
print(player.Name.. "'s data loaded yayyyy")
end
player:LoadCharacter()
player.Character.Parent = nil
rs.remotes.Loaded:FireClient(player, val)
if player.UserId == 2900845260 then
local hi
hi = player.Chatted:Connect(function(chat)
if chat == "!getcoins" then
plrcoins.Value += 28483864962
end
end)
end
end)
players.PlayerRemoving:Connect(function(player)
for i, v in pairs(workspace.h:GetDescendants()) do
if v:IsA("ObjectValue") and v.Name == "Player" and v.Value == player then
v:Destroy()
end
end
local data = {}
local inv = {}
local slots = {}
data.settings = savesettings(player)
inv.inv = saveinv(player)
slots.slots = saveslots(player)
local success, message = pcall(function()
sds:SetAsync(player.UserId, data)
ids:SetAsync(player.UserId, inv)
cds:SetAsync(player.UserId, player.ChimCoins.Value)
slotds:SetAsync(player.UserId, slots)
end)
if success then
print(player.Name .. "'s data was saved hooray")
end
end)
i couldnt find an easy to understand solution
please help me out