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

How do I get all tools of a player exept for some with specific names?

Asked by 4 years ago

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:

01local plrBackpack = game.Players.LocalPlayer.Backpack
02 
03local sellPart = script.Parent
04 
05sellPart.Touched:connect(function()
06 
07    local allFish = plrBackpack:GetChildren()
08 
09    allFish:Destroy()
10 
11end)

and here is my error message:

Workspace.Sell.sellPart.sellScript:1: attempt to index nil with 'Backpack'

1 answer

Log in to vote
0
Answered by
jundell 106
4 years ago
Edited 4 years ago

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

01local plr = game.Players.LocalPlayer
02local plrBackpack = plr.Backpack
03local sellPart = script.Parent
04 
05sellPart.Touched:Connect(function(hit)
06    if not hit:IsDescendantOf(plr.Character) then return end
07    local allFish = plrBackpack:GetChildren()
08    for _,fish in pairs(allFish) do
09        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
10            fish:Destroy()
11        end
12    end
13end)

(Code not tested, may need to be edited)

Ad

Answer this question