Delete Item from Inventory/Backpack through Click

Hello everyone,

I have a Suitcase in my Inventory/Backpack and when i Click into the Dresser i want the Suitcase to get Delted out of my Inventory. In addition some items appear in the Dresser. Thats works fine but the Suitcase isnt deleted sadly. Whats wrong? Can anybody help?

Here is the Script:

local suitcaseName = "Suitcase"
local Click = script.Parent.ClickDetector

local function onButtonPress(player)
	-- Check if the player has the "Suitcase" item in their inventory
	local character = player.Character
	local backpack = player:FindFirstChild("Backpack")

	if character and backpack then
		local suitcase = backpack:FindFirstChild(suitcaseName) or character:FindFirstChild(suitcaseName)

		if suitcase then
			-- If the suitcase is found, delete it
			suitcase:Destroy()
			print("Suitcase deleted from player's inventory.")
			-- Optionally, you can add additional logic here
			-- For example, play a sound or perform other actions
		else
			print("Suitcase not found in player's inventory.")
		end
	end
end

Click.MouseClick:Connect(function()
	-- Get the player who clicked the button
	local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)
	if player then
		-- Call the function to delete the "Suitcase" item for the pressing player
		onButtonPress(player)
	end

	-- Additional functionality for the button click
	script.Parent.Parent.PartPart.PutSuitcaseBack.Enabled = true
	script.Parent.Parent.PartPart.WardrobeFilled.ContentNotTransparent.Enabled = true
end)


^ Filled Dresser


^The Part thats Clicked

Thank you guys for your Hekp alrerady <3

Forgot to add the File:

Suitcase.rbxl (322.2 KB)

You didn’t even call it the MouseClick event?

Click.MouseClick:Connect(onButtonPress)
1 Like

Tried after Posting this but i get a
Workspace.Schrank.Suitcase.Suitcase Deltetion:29: attempt to index nil with 'Parent etc.

I review for Typos but its driving me absolutley nuts today.

Did you get any more errors along with it? Just try copy pasting the full output

Try to insert a Break Point and debug step by step.

I’m still new to scripting but, normally with the MouseClick event you would already pass ‘player’ as a parameter. Try:

Click.MouseClick:Connect(function(player)
    if player then
        onButtonPress(player)
    end
end)
1 Like

Yep, that’s correct.

Click.MouseClick:Connect(function(player)
	onButtonPress(player)

	-- Additional functionality for the button click
	script.Parent.Parent.PartPart.PutSuitcaseBack.Enabled = true
	script.Parent.Parent.PartPart.WardrobeFilled.ContentNotTransparent.Enabled = true
end)
1 Like

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