Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
JellyYn 70
6 years ago
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

Log in to vote
1
Answered by
Nonaz_jr 439 Moderation Voter
6 years ago
Edited 6 years ago

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!

Ad
Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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!

1
You forgot to do [apple.Name] User#19524 175 — 6y
0
solving... and thanks Leamir 3138 — 6y
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. JellyYn 70 — 6y
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) Leamir 3138 — 6y
View all comments (2 more)
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 Nonaz_jr 439 — 6y
0
Just a detail. Another way I solved this in the past is just to use humanoid:UnequipTools first. :D Nonaz_jr 439 — 6y

Answer this question