Inventory System Bug

ToolSelectedRE.OnServerEvent:Connect(function(plr, tools) -- tools is the table you're going to send
	if not tool or typeof(tool) ~= "Instance" then return end -- we still want this here just in case

    for _, tool in pairs(tools) do
		local ownedTool = plr.OwnedTools:FindFirstChild(tool.Name)
	
		if not ownedTool then
			print(plr.Name," is buying the ",tool.Name," tool.")
	
			local price = tool.Price.Value
			local coins = plr.leaderstats.Coins
	
			if price <= coins.Value then
				coins.Value -= price
	
				local newTool = tool:Clone() do
					newTool.Parent = plr.OwnedTools
				end
			end
		elseif ownedTool then
			if plr.Backpack:FindFirstChild(tool.Name) == nil then
				print(plr.Name," equipped ",tool.Name," tool.")
	
				local new = tool:Clone()
				new.Parent = plr.Backpack
			elseif plr.Backpack:FindFirstChild(tool.Name) then
				print("triggered")
	
				local deleted = plr.Backpack:WaitForChild(tool.Name)
				deleted:Destroy()
			end
	    end
    end
end)

Tell me if this works or not.

So, its acting even weirder now (i’ll try to explain the best I can). So instead of last time where when all 2 of the buttons said “unequip” and you would have to click each of them 2 times to work, you can now click one button just once to change back to “equip” but the other one will take two clicks. This has nothing to do with the frame, its whatever button says “unequip” last will take two clicks. Does that make sense?

Yeah, I think I understand. In the script you provided in post 1, did you do something like table.insert(SelectedTrail, VAL)?

DID YOU CHANGE IT TO**

1 Like

No, I have not changed it to a table yet. Should I?

1 Like

This is the full script:

function updateInventory()

	for i, child in pairs(invFrame.ShopGUI.Tools:GetChildren()) do
		if child:IsA("ImageLabel") then child:Destroy() end
	end


	local ownedTrails = ownedTrailsFolder:GetChildren()
	print(ownedTrails)

	table.sort(ownedTrails, function(a, b)
		return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
	end)


	for i, tool in pairs(ownedTrails) do
		local item = script.InvItem:Clone()
		item.Image = tool:WaitForChild("Folder").ImageLabel.Image
		item.SelectButton.Text = "Equip"
		item.Title.Text = tool.Name
		item.Visible = true



		item.Parent = invFrame.ShopGUI.Tools






		item.SelectButton.MouseButton1Click:Connect(function()
			local price = tool.Price.Value
			local coins = player.leaderstats.Coins.Value
			if item == SelectedTrail then
				item.SelectButton.Text = "Equip"

				SelectedTrail = nil
			else
				if player.OwnedTools:FindFirstChild(tool.Name) or (price <= coins) then
					item.SelectButton.Text = "Unequip" 
				else
					item.SelectButton.Text = "Unequip"
				end
				

				SelectedTrail = item
			end

			TrailSelectedRE:FireServer(tool)
		end)
	end




end		

I realize now I was missing some parts which could have made some variables confusing.

Before I give you the full script, I need to tell you about another problem.
You see the function where you click SelectButton? When updateInventory runs again, those functions are still there.

If we use a dictionary, we would be able to remove and disconnect this function.

also I changed everything to a table

local buttonDictionary = {}
local clonedToItem = {}

function updateInventory()
	for i, child in pairs(invFrame.ShopGUI.Tools:GetChildren()) do
		if child:IsA("ImageLabel") then
            child:Destroy() 
            buttonDictionary[child.SelectButton]:Disconnect() -- removing it
            buttonDictionary[child.SelectButton] = nil 
        end
	end

    for i, v in pairs(clonedToItem) do
       clonedToItem[i] = nil
    end

	local ownedTrails = ownedTrailsFolder:GetChildren()
	print(ownedTrails)

	table.sort(ownedTrails, function(a, b)
		return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
	end)

	for i, tool in pairs(ownedTrails) do
		local item = script.InvItem:Clone()
		item.Image = tool:WaitForChild("Folder").ImageLabel.Image
		item.SelectButton.Text = "Equip"
		item.Title.Text = tool.Name
		item.Visible = true

        clonedToItem[item] = tool

		item.Parent = invFrame.ShopGUI.Tools

		buttonDictionary[item.SelectButton] = item.SelectButton.MouseButton1Click:Connect(function() -- adding it
			local price = tool.Price.Value
			local coins = player.leaderstats.Coins.Value

			if item == SelectedTrail then
				item.SelectButton.Text = "Equip"

				table.remove(SelectedTrail, table.find(SelectedTrail, item))
			else
				if player.OwnedTools:FindFirstChild(tool.Name) or (price <= coins) then
					item.SelectButton.Text = "Unequip" 
				else
					item.SelectButton.Text = "Unequip"
				end
				

				table.insert(SelectedTrail, item)
			end

            local finalTable = {}

            for i, v in pairs(SelectedTrail) do
                table.insert(finalTable, clonedToItem[v])
            end

			TrailSelectedRE:FireServer(finalTable)
		end)
	end
end		

There is an error on this line right here because SelectedTrail doesn’t exist anymore. What should I change it to?

if item == SelectedTrail then

if table.find(SelectedTrail, item) then

I changed the script above to fit, also.

There is still a red line under “SelectedTrail.” Also, could you elaborate on the future issue and what this could cause?
Edit: I see what you mean about the problem, nevermind.

Im confused about the red line under SelectedTrail because I define it in my variables.

Where is this line? Also which script is it in

Its in the updateInventory() script.
Its on this line that you gave me:

if table.find(SelectedTrail, item) then

Let me see the variable where SelectedTrail is defined. It should automatically be

local SelectedTrail = {}

Show me a picture of the red line in the code if it doesn’t work.

I changed the variable to that. The issue now is that its not finding SelectButton in the cloned InvFrame.

Did you check post 14 again? in the bottom I changed it a little.

There is still the same problem on this line:

buttonDictionary[child.SelectButton]:Disconnect() -- removing it
			buttonDictionary[child.SelectButton] = nil 

It keeps saying that Selectbutton is not a child of invItem when it is. Maybe there is a problem with cloning?
By the way, thanks for being the only one helping me, I appreciate you.

Check in the frame in-game to see if there’s any other frames without SelectButton

The invframe that clones isn;t even cloning so I can’t check.

I know why. It’s because we’re removing the button before that happens. Move that above the line where you’re removing it.