Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Destroying item from players backpack not working? [HELP PLEASE]

local apple = script.Parent
local p = game.Players.LocalPlayer
apple.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		p.Backpack:WaitForChild("Apple"):Destroy()
	end)
end)

I have it so the apple's parent turns in to the players Backpack when clicked from the workspace. I want it to when you click, it removes it from the players Backpack for good. Any ideas on how to fix this?

2 answers

✓ Accepted answer

Hi, as Leamir points out the item when equiped is in the character, not the backpack. So as explained in his answer and the comments either check there instead or unequip the item before looking for it in backpack and destroying it.

I would like to add that your script is not FilteringEnabled compatible: You cannot destroy items from a localscript. However, to fix it is easy, as you do not actually need to listen to the mouseclick but can instead use apple.Activated (which triggers on mouseclick while equiped serverside.

Also despite my extensive comments to make sure you find the apple in the right place and it exists, just use script.Parent:Destroy()

X) i wasn't thinking ;)

so move the code to a script (not a localscript any more) and make it:

local apple = script.Parent
apple.Activated:Connect(function()
  apple:Destroy()    
end)

There, that's much cleaner AND filteringEnabled for free!

Hello, JellyYn!

Your script don't works because when player equips an item, its goes to its character(game.Players.LocalPlayer.Character), so you have to get its character in workspace, like I did!

local apple = script.Parent
local p = game.Players.LocalPlayer
apple.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
	game.Workspace[p.Name][apple.Name]:Destroy()
    end)
end)

Good Luck with your games!

6 comments

1

You forgot to do [apple.Name]

0

solving... and thanks

0

It works! The only thing I had to change for it to work (for me, at least, with FE on), was [apple]. I had to change it to [p.name]:WaitForChild("Apple"):Destroy() and it worked. I will accept this answer anyway because it helped a lot.

0

You can just use '[p.name]:FindFirstChild("Apple"):Destroy()' This don't will break your script if player don't have the item(ik, its a little impossible, but)

0

that won't help, as you are calling a function on a nil item. do local apple = game.Workspace[p.name]:FindFirstChild("Apple"); if apple then apple:Destroy() end it will still break when the character just died or bug if you happen to have an object with the player name in workspace.better use if player.Character and player.Character:FindFirstChild("Apple") then player.Character.Apple:Destroy() end

0

Just a detail. Another way I solved this in the past is just to use humanoid:UnequipTools first. :D

This discussion is preserved as it was. New answers, comments, and votes are closed.