Just loop all children inside folder and save the value
game.Players.PlayerRemoving:Connect(function(player)
if player.Storage.OwnsServer.Value == true then
local success, err = pcall(function()
local tableToSave = {}
for i, item in pairs(player.Storage:GetChildren()) do
Tabletosave[tem.Name] = item.Value
print(i)
end
if player.Storage.OwnsServer.Value == true then
serverStorage:SetAsync(player.UserId.."-ServerStorage", tableToSave)
end
end)
if success then
print("Successfully saved data")
else
warn(err)
end
else
print(player.Name.." doesn't own a server!")
end
end)
I would say you need to utilize a dictionary instead of a simple array when trying to load and save specific data so when using GetAsync the script will understand which value is which.
Try this:
local datastoreservice = game:GetService("DataStoreService")
local serverStorage = datastoreservice:GetDataStore("ServerStorage")
game.Players.PlayerAdded:Connect(function(player)
local storage = Instance.new("Folder")
storage.Name = "Storage"
storage.Parent = player
local ownsServer = Instance.new("BoolValue")
ownsServer.Name = "OwnsServer"
ownsServer.Parent = storage
local serverName = Instance.new("StringValue")
serverName.Name = "ServerName"
serverName.Parent = storage
local owner = Instance.new("StringValue")
owner.Parent = storage
owner.Name = "Owner"
local data
local success, err = pcall(function()
data = serverStorage:GetAsync(player.UserId.."-ServerStorage")
end)
if (data) then
print("Data successfully loaded")
for itemName, itemValue in pairs(data) do
player.Storage[itemName].Value = itemValue
print(itemName, "set to", itemValue)
end
else
print("No data found for the player")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
if player.Storage.OwnsServer.Value == true then
local success, err = pcall(function()
local tableToSave = {}
tableToSave["OwnsServer"] = player.Storage.OwnsServer.Value
tableToSave["ServerName"] = player.Storage.ServerName.Value
tableToSave["Owner"] = player.Storage.Owner.Value
if player.Storage.OwnsServer.Value == true then
serverStorage:SetAsync(player.UserId.."-ServerStorage", tableToSave)
end
end)
if success then
print("Successfully saved data")
else
warn(err)
end
else
print(player.Name.." doesn't own a server!")
end
end)
Also, make sure to check if this conditional statement is true; it could technically not error, but still be false and not save data in the first place.
if player.Storage.OwnsServer.Value == true then
serverStorage:SetAsync(player.UserId.."-ServerStorage", tableToSave)
end