For loop not running

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!

1 Like

print the length of the pets array.

print(#player.PetInventory:GetChildren())
-- loop

if it is 0, it won’t run.
If PetInventory has children, check to make sure they exist on the server.
If none of those work, it might be some deeper problem.

2 Likes

Thanks for the help! It just didn’t add it on the server, only the client. Would RemoteEvents be the best way to send it to the server and add it?

1 Like

yeah, remote events or remote functions would be the best way, just send the name of the pet (if the player already owns it) and whatever info that can be trusted by the client.

1 Like

As @Styre_x said, remote events will be the best way to send the data for each pet to the server. When the remote event is fired from the client you can send data with the event to the server which can be used to store the correct pet. Using remote events and saving server-side will also benefit you by adding an extra layer of security if you validate that the player can receive the pet first.

While I assume you’re comfortable with RemoteEvents, in the case that you’re not here is where you can get started.

1 Like