For Loop Not Stopping While Looping Through a Datastore Table

I have this segment of code which gets a table from a datastore. Once gotten, it loops through the table and gets the values of it. Once it has the values, it calls a function and it starts to repeat indefinitely. I’m not sure why this does this and the table is only 1 entry.

No the functions that load the table and execute the for loop are not being called within each other and no the table is not a string before anything happens.

-- horbar code 

itemHandler.spawnHotBar = function(player, itemname, slot)
	local inventoryModule = require(players:WaitForChild(player.Name):WaitForChild('Inventory'))
	local inventory = inventoryModule.inventory
	local hotBar = inventory[3]
	local itemid
	for int, item in ipairs(inventory[1]) do
		if item['ItemName'] == itemname then
			item['Hidden'] = true
			itemid = item['ItemId']
		end
	end
	local insertTable = {
		['ItemName'] = itemname,
		['ItemId'] = itemid,
		['Slot'] = slot
	}
	table.insert(hotBar['Items'], insertTable)
	hotBar['ItemCount'] = hotBar['ItemCount'] + 1
	game:GetService('ReplicatedStorage'):WaitForChild('Remotes'):WaitForChild('UpdateHotBar'):FireClient(player, itemname, slot, true)
end

-- load hotbar from datastore 

temHandler.loadHotBar = function(player)
	local inventoryModule = require(players:WaitForChild(player.Name):WaitForChild('Inventory'))
	local hotbar = inventoryModule.inventory[3]
	for _, item in ipairs(hotbar['Items']) do
		itemHandler.spawnHotBar(player, item['ItemName'], item['Slot'])
		task.wait(0.01)
	end
end

-- where everything is called 

players.PlayerAdded:Connect(function(player)
	local inventoryModule = serverStorage:WaitForChild('Modules'):WaitForChild('Inventory'):Clone()
	inventoryModule.Parent = player
	itemHandler.spawnStats(player)
	if config:GetAttribute('LoadPlayerData') then
		local storeKey = player.Name..'_INV_DS'
		if dataStoreHandler.checkDataStore('playerInstanceInventory', storeKey) then
			local data = dataStoreHandler.getDataStore('playerInstanceInventory', storeKey)
			local inventory = require(inventoryModule).inventory
			inventory[1] = data['Inventory']
			inventory[2] = data['InventoryData']
			inventory[3] = data['HotBarData']
			itemHandler.loadInventory(player)
			itemHandler.loadHotBar(player)
			print(inventory)
		else

--  this is what the table would look like normally

			local setTable = {
				['Inventory'] = {},
				['InventoryData'] = {
					['MaxItems'] = 250,
					['CurrentItems'] = 0,
					['MaxDifferent'] = 40,
					['CurrentDifferent'] = 0},
				['HotBarData'] = {
					['Items'] = {},
					['ItemCount'] = 0
				}
			}
			dataStoreHandler.setDataStore('playerInstanceInventory', storeKey, setTable)
		end
	end
end)

I’m not exactly sure on what is wrong or what is happening with my code to where is repeats.

Can you please put a comment on where the issue occurs?

Here are the lines of code that I feel are causing the issue.

Wait, I think I figured it out.
Every time I call:

itemHandler.updateHotBar(player, itemname, slot)

It adds an entry to the table which the for loop will then loop over again.

This happens in:

table.insert(hotBar['Items'], insertTable)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.