Deleting Table Items

Hey there, so awhile ago I thought this worked, but I now see that it does not.

When a player in my game rebirths, their pets’ upgrades get reset to 0, and I thought I had figured it out, but it isn’t actually doing anything.

All items would show up in the table like this example:
Table

_G.petUpgradeAmount[player.userId]

An item that would appear in the table

["CatPet"] = 0

I am trying to reset them all through this code

for i, ITEM in pairs(_G.petUpgradeAmount[player.userId]) do
	_G.petUpgradeAmount[player.userId][ITEM] = 0
	print(_G.petUpgradeAmount[player.userId][ITEM])
end

For some reason the print statement always prints 0, but then I print the whole table after the for loop, and they aren’t changed at all.

Does anyone know why this is the case?

This is likely occurring from the fact that you use the value, instead of the index.

A for loop uses the index, and the value, which is the two values you can use when iterating through a table. In this instance you are using the value of the table instead of the index.

Try using the index instead, like so:

for index, ITEM in pairs(_G.petUpgradeAmount[player.userId]) do -- index is the index, while ITEM is the value of the current index. For example, the ITEM might be value 30 (the pet's level), while the index might be "CatPet", which is the name of the pet.
	_G.petUpgradeAmount[player.userId][index] = 0
	print(_G.petUpgradeAmount[player.userId][index])
end

I hope this helps!

1 Like