✓ 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!
6 comments
You forgot to do [apple.Name]
solving... and thanks
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.
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)
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
Just a detail. Another way I solved this in the past is just to use humanoid:UnequipTools first. :D