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

Why is this not removing the item from the players Backpack?

I have a sell GUI and create a BoolValue inside of an item when it is sold and name it "flag" and set it to true. It is removing the item from the gui and giving the player the gold, but it is not removing the item from the players Backpack. I put a print inside the script to see if it is finding the item and it is. I look inside the players Backpack and the flag is on the item, but it is not finding the flag and removing the item.

local dataStore = require(game.ServerStorage.DataStore)
local RS = game:GetService("ReplicatedStorage")
local event = RS.Remotes:WaitForChild("Sell")
event.OnServerEvent:Connect(function(plr, amount)
	plr.leaderstats.gold.Value = plr.leaderstats.gold.Value + amount
	dataStore:AddStat(plr, "gold", amount)
	    for _, item in pairs(plr.Backpack:GetChildren()) do
		   print(item)
	           if item:FindFirstChild("flag") then
	 	           print("flag found")
		           plr.Backpack.item:Remove()
     	   	    end
	    end
end)

3 comments

0

maybe try using :destroy() instead of :Remove() that may work but im not sure

0

I tried that already, but that didn't work either. Thanks though for answering. :)

0

also as a heads up: if the item is being held, it's found inside of their character instead of Backpack

3 answers

Use :Destroy() instead of :Remove()

3 comments

0

or you can set the parent to nil

0

Oh ugh, I see what I am doing, I am flagging it in the script in the GUI, so the Server never sees the flag. I need to pass the item in the server event. But, thanks for the answers!!!

0

I am flagging it in a Local Script in the GUI I meant to say, so yeah the Server never sees the flag.

item is actually the object, so in your script you're actually trying to find a child of the children in the backpack called "flag"

local dataStore = require(game.ServerStorage.DataStore)
local RS = game:GetService("ReplicatedStorage")
local event = RS.Remotes:WaitForChild("Sell")
event.OnServerEvent:Connect(function(plr, amount)
    plr.leaderstats.gold.Value = plr.leaderstats.gold.Value + amount
    dataStore:AddStat(plr, "gold", amount)
        for _, item in pairs(plr.Backpack:GetChildren()) do
           print(item)
               if item.Name == "flag" then
                   print("flag found")
                   plr.Backpack.item:Destroy()
                end
        end
end)

and yes, use Destroy() instead of Remove(), hope this helps

1 comment

0

Thanks so much for answering, But I figured it out, I am flagging it in a Local Scrip tin the GUI, so the Server never sees the flag. If I had posted my GUI script, I am sure you guys would have spotted that right away. It took me a while. LOL

If it's in their backpack already, then it should work. But, when a player equips an item, it moves to their character. That may be your issue. Try referencing the tool from the character.

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