Inventory system : Left click to equip, right click to drop

Hey !
i’m currently making an inventory system when, by pressing tab, the gui with the player inventory pop up. Now if he play left click on the item, it equips it, when he right lcik on it, it drops it. Here is the code :

local equipped = script.Parent.Handler.Equipped
local selected = script.Parent.Handler.Selected
local location = script.Parent.Handler.Location
local player = game.Players.LocalPlayer
local character = player.Character

local items = {}
local buttons = {}
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false) -- Makes the original backpack gui invisible

function search(location)
	for i,v in pairs(location:GetChildren()) do -- Find all item in a specific location
		if v:isA("Tool") then -- If the item found is a "Tool"
			table.insert(items,v) -- We're going to put all the tools found in a table.
		end
	end
end

function refresh()
	for i,v in pairs(buttons) do -- Finds all items in the table
		v:Destroy() -- Destroy 'em all
	end
	for i,v in pairs(items) do -- Finds all items in the table
		local button = script.Sample:Clone() -- clones the sample button inside the localscript
		button.Name = v.Name -- sets the cloned button's name to the name of the item
		button.LayoutOrder = i
		button.Parent = script.Parent.Handler -- Sets the parent of the cloned button to the handler
		button.Image = v.TextureId -- Sets the image of the button to the texture id of the tool
		table.insert(buttons,button) -- Inserts the button to our table "buttons"
		button.MouseButton1Click:connect(function()
			if script.Parent.Handler.Selected.Value == nil or script.Parent.Handler.Selected.Value ~= v then -- Checks if the selected value is nothing or if the selected value is not the button
				script.Parent.Frame.ItemName.Text = v.Name -- Sets the TextLabel's Text to the name of the tool/button
				script.Parent.Frame.ImageLabel.Image = v.TextureId -- Sets the image label's image to the texture id of the tool
				script.Parent.Handler.Selected.Value = v
				if equipped.Value == nil or equipped.Value ~= selected.Value then -- Just the same as the last one
					character.Humanoid:UnequipTools() -- Forces the player to unequip the tool that they equipped
					if location.Value == player.Backpack then
						character.Humanoid:EquipTool(selected.Value)
						equipped.Value = selected.Value
						script.Parent.Text = "Unequip"
					end
				else
				end
			end
		end)
	end
end

function backpackRefresh()
	items = {}
	search(character)
	search(player.Backpack)
	refresh()
end

backpackRefresh()

player.Backpack.ChildAdded:connect(backpackRefresh)
player.Backpack.ChildRemoved:connect(backpackRefresh)

character.ChildAdded:connect(backpackRefresh)
character.ChildRemoved:connect(backpackRefresh)

Note that it isn’t mine
– Useless word –

Do this script have a issue? If it’s your creation, put it in #help-and-feedback:cool-creations

1 Like

It doesn’t equip the tool, that’s why I put it in scripting support

Can you please show me the output?

You destroy all the items and put them in a table after, but since they are destroyed, they do not exist anymore.

1 Like

That wouldn’t contribute to anything since they are remade afterwards, it is something else that’s causing it

@Black_Albi please add prints inside of the MouseButton1Click event and tell us where the code stops executing

The only thing that would help us is that he add prints and if one print don’t run then we know where is the issue. And then we would see the output.

Adding print
– Don’t mind this–

That’s literally what I had mentioned

Not to be rude but you did never mention the output. :slight_smile:

I mentioned that he should add prints and tell us where the code stops executing, how is he supposed to know where the code stops executing without looking at the output

Nah he asked me to add print, but it’s the same thing

Let’s not go off topic before another person flag us.

Provide the output please it’ll display any errors.

Did you find any issues? Also, she

Ye sorry I always forget about this ^^’
I numeroted the step and it seems that the script stop working at the number “6” on 8

Could you show us the code after adding prints

Can you show us a screenshot of the exact line and below it?

Quote the script line so we can see it, also send us the full output.