So I have a pet system, and it saves (or should), but it saves a blank table, because after some debugging, I figured out this for loop doesn’t run:
for i,pet in pairs(player.PetInventory:GetChildren()) do
print('inserting pets into table')
table.insert(inventory, pet.Name)
print(inventory, #pet)
end
There’s no reason at all that it shouldn’t run, but does anybody know why this happens?
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local data = game:GetService("DataStoreService")
local storage = game:GetService("ServerStorage")
function saveProgress(player)
local saveData = data:GetDataStore('saveData', player.UserId)
local petData = data:GetDataStore('petData')
saveData:SetAsync(player.UserId..'thetest', player.testingcode.Value)
saveData:SetAsync(player.UserId..'money', player.leaderstats.Coins.Value)
saveData:SetAsync(player.UserId..'wins', player.leaderstats.Wins.Value)
if player:FindFirstChild('PetInventory') then
local inventory = {}
for i,pet in pairs(player.PetInventory:GetChildren()) do
print('inserting pets into table')
table.insert(inventory, pet.Name)
print(inventory, #pet)
end
local success, errorMessage = pcall(function()
print(petData:GetAsync(player.UserId.."-pet")[1])
petData:SetAsync(player.UserId.. "-pet", inventory)
end)
if success then
print('data saved!')
else
print(errorMessage)
end
end
end
game.Players.PlayerRemoving:Connect(saveProgress)
game:BindToClose(function()
for i,player in pairs(game.Players:GetPlayers()) do
saveProgress(player)
end
end)
The for lop just doesn’t run at all, but everything before and after does. if do insert something into a table, it works and saves, the for loop just doesn’t run at all. Here’s a similar topic on stack overflow that I found.
Any help would be appriciated!