Table clears when trying to remove hotbar slot in custom hotbar module

I am making a custom hotbar system, everything works well but for some reason when I try removing a slot it clears the currentHotbars table

I tried adding debug prints at the places where the function is fired and the table is only cleared when the .remove() function is fired

local hotbar = {}

local slot = script.Slot
local currentHotbars = {}

local check = require(script.StartCheck)

local function getPlayerHotbar(player)
	print(player)
	print(currentHotbars)
	for _, hot in pairs(currentHotbars) do
		print(hot)
		if hot.Player == player.UserId then
			print("is player")
			local playerHotbar = hot.HotBar
			return playerHotbar
		end
	end
end

local function checkForDuplicates(slotnumber, hotbar)
	if not hotbar:FindFirstChild("Frame") then 
		return true 
	end

	for _, slot in pairs(hotbar.Frame:GetChildren()) do
		print(slot.Name)
		if string.find(slot.Name, slotnumber.."_Slot") then
			return true
		end
	end

	return false
end


function hotbar:Start(player)
	local playerHotbar = getPlayerHotbar(player)
	if playerHotbar then return end
	
	if not player then return end
	local pgui = player:FindFirstChild("PlayerGui")
	if not pgui then return end
	
	local hotbarUI = script.Hotbar:Clone()
	hotbarUI.Parent = pgui
	table.insert(currentHotbars, {Player = player.UserId, HotBar = hotbarUI})
	
	player:GetPropertyChangedSignal("Parent"):Connect(function()
		if not player.Parent then
			check.Remove(player)
			local playerHotbar
			for i, v in pairs(currentHotbars) do
				if v.Player == player.UserId then
					table.remove(currentHotbars, i)
					break
				end
			end
		end
	end)
	
	--coroutine.wrap(function()
	--	while true do
	--		task.wait()
	--		print(currentHotbars)
	--		if #currentHotbars == 0 then
	--			break
	--		end
	--	end

	--end)()
	
	return check.Ready(player, hotbar, hotbarUI)
end

function hotbar.add(tool, slotNumber, player)
	if not player then return end
	if not tool then return end
	if not slotNumber then return end
	
	if slotNumber > 9 and slotNumber ~= 0 then return end
	if slotNumber > 9 then slotNumber = 0 end
	
	
	local playerHotbar = getPlayerHotbar(player)
	if not playerHotbar then return end
	
	local dupe = checkForDuplicates(slotNumber, playerHotbar)
	if dupe == true then return end
	
	local hotslot = slot:Clone()
	hotslot.ToolName.Text = tool.Name
	hotslot.ToolNumber.Text = slotNumber
	hotslot.Icon.Image = tool.TextureId
	hotslot.Tool.Value = tool
	if typeof(slotNumber) == "number" then
		hotslot.Name = slotNumber.."_Slot"
	end
	
	hotslot.Parent = playerHotbar.Frame
end

function hotbar.remove(slotNumber, player)
	if not player then return end
	if not slotNumber then return end
	
	print("passed checks")

	if slotNumber > 9 and slotNumber ~= 0 then return end
	if slotNumber > 9 then slotNumber = 0 end
	
	print("passed 2")

	local playerHotbar = getPlayerHotbar(player)
	if not playerHotbar then return end
	
	print("hotbar")

	for _, slotChild in pairs(playerHotbar.Frame:GetChildren()) do
		print(slotChild)
		if slotChild.Name == slotNumber .. "_Slot" then
			print("isname")
			slotChild:Destroy()
		end
	end
end

return hotbar

I also tried checking for the userid instead of the player instance and it make no changes.

1 Like

idk if this is related, but the Parent property of a player can’t be changed, so this code will never run. Also in your hotbar.remove function, you’re not using currentHotbars so not sure if that’s the issue

	player:GetPropertyChangedSignal("Parent"):Connect(function()
		if not player.Parent then
			check.Remove(player)
			local playerHotbar
			for i, v in pairs(currentHotbars) do
				if v.Player == player.UserId then
					table.remove(currentHotbars, i)
					break
				end
			end
		end
	end)