So I am making a fishing simulator game, but I don't want to sell all items (The rods), but only the fish, here is my code:
local plrBackpack = game.Players.LocalPlayer.Backpack local sellPart = script.Parent sellPart.Touched:connect(function() local allFish = plrBackpack:GetChildren() allFish:Destroy() end)
and here is my error message:
Workspace.Sell.sellPart.sellScript:1: attempt to index nil with 'Backpack'
It looks like you are trying to get LocalPlayer in non-LocalScript. First, use a LocalScript instead, then fix you need to fix the removal of the fish. You are trying to :Destroy() a table (plrBackpack:GetChildren()). Instead, loop through the table and destroy each fish in it.
Lastly, you should add a check to make sure it’s the player who touches sellPart.
Edit: Oh, I forgot to answer your actual question. To exclude any child, just do something in the loop like: if child.Name ~= “Rod” then child:Destroy() end
local plr = game.Players.LocalPlayer local plrBackpack = plr.Backpack local sellPart = script.Parent sellPart.Touched:Connect(function(hit) if not hit:IsDescendantOf(plr.Character) then return end local allFish = plrBackpack:GetChildren() for _,fish in pairs(allFish) do if fish.Name == 'Fish' then -- change Fish to whatever the name of the fishes are, or if they have different names then do as I suggested in the explanation fish:Destroy() end end end)
(Code not tested, may need to be edited)