Drop system when holding the tool is glitchy

Hey !
I finnaly finish this inventory system (EmbatTheHybrid if you read this, Thanks a lot for your amazing help !) So when the player doesn’t handle the tool, No problem, drop works fine. When the player hold it, the icons delete, but the items isn’t correctly deleted from player backpack or is still welded to player hand. How can I fix this? here’s the code part :

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") and #items <= 8 then -- If the item found is a "Tool"
			table.insert(items,v) -- We're going to put all the tools found in a table.
			print("1")
		end
	end
end

function refresh()
	search(location)
	for i,v in pairs(buttons) do -- Finds all items in the table
		v:Destroy() -- Destroy 'em all
		print("2")
	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"
		print("3")
		button.MouseButton2Click:Connect(function()

				button:Destroy()
				v.Parent = workspace
				v.Positon = player.Character.HumanoidRootPart.Position + Vector3.new(0.1,0,0)

It's only a small part of the script, that's why there aren't end
2 Likes

I think it would better if you show us the full code again since it doesn’t really help like this

Sure ! Here it is, but the main part of the code is used for equipping the tool



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") and #items <= 8 then -- If the item found is a "Tool"
			table.insert(items,v) -- We're going to put all the tools found in a table.
			print("1")
		end
	end
end

function refresh()
	search(location)
	for i,v in pairs(buttons) do -- Finds all items in the table
		v:Destroy() -- Destroy 'em all
		print("2")
	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"
		print("3")
		button.MouseButton2Click:Connect(function()
			if v.Parent == player.Character then
				button:Destroy()
				v.Positon = player.Character.HumanoidRootPart.Position + Vector3.new(0.1,0,0)
				player.Character:FindFirstChildWhichIsA("Tool"):Destroy()
				v.Parent = workspace

			elseif v.Parent == game.Players.LocalPlayer.Backpack then
				button:Destroy()
				v.Parent = workspace
				v.Positon = player.Character.HumanoidRootPart.Position + Vector3.new(0.1,0,0)
			end

		end)
		button.MouseButton1Click:connect(function()
			print("4")
			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
				print("5")
				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
					print("6")
					character.Humanoid:EquipTool(selected.Value)
					equipped.Value = selected.Value
					print('7')
				end
			else
				character.Humanoid:UnequipTools() -- Forces the player to unequip the tool that they equipped
				script.Parent.Handler.Selected.Value = nil
				script.Parent.Frame.ItemName.Text = "Name"
				equipped.Value = nil
			end
		end)
	end
end



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

backpackRefresh()

player.Backpack.ChildAdded:connect(backpackRefresh)
player.Backpack.ChildRemoved:connect(backpackRefresh)
search(location)
character.ChildAdded:connect(backpackRefresh)
character.ChildRemoved:connect(backpackRefresh)

You’re destroying the tool and then trying to set its parent

She is right. You destroy the tool then set his parent. But since the tool is destroyed and not re-created after, it’s return a error.

I tried without it, but the problem doesn’t come from this, because even when I destroy it, it doesn’t delete it from the player hand

This is the reason why I tried to put this
I put it at the end of the statement

Can’t you just use player.Character.Humanoid:UnequipTools() and then set the position of the tool?

Oh and a little precision the tool is unequiped, cuz I can equip other tool, this is like a “ghost block”

Sorry the screen isn’t well taked, the inventory is empty, but this is what happens :

Again, can’t you use player.Character.Humanoid:UnequipTools() and then set the position and Parent?

Wait imma try but I think I did before doing this method
– Edit x2 –
Here is what it does :

So to get this straight, it does drop it, but it doesn’t remove it from your hand?

Yep, I checked myself in explorer. It isn’t in backpack ar ein player character

That’s…Odd? It should successfully remove it, could you check al the contents of your Character when you have a tool equipped?

The humadois model.
Vest and LVest value
Clothes and body color
– Edit –
The tool is still on the character

Wait I think realise the issue, it’s somthing related to this

It refreshes the backpack when a child was added or removed from the player, which is happening when you destroy the tool, I think this is related