FindFirstChild only works one time

Hi i am working on a gun dealer for roleplay games but i have one issue i can buy a gun once but the second time i try i get this message: attempt to index with ‘findfirstchild’
I did search for a solution but didn’t find one. :WaitForChild isn’t working to.
(Sorry for my bad English i am Dutch :3 and this is my first time using DevForum so if i make a mistake please say it)

for i, button in pairs (script.Parent.ScrollingFrame:GetChildren()) do
	if button.Name == "TextButton" then
	button.MouseButton1Click:Connect(function()
			local Price = knop.Price.Value
			local ItemName = knop.ItemName.Value
			local Item = game.ServerStorage.Guns:FindFirstChild(ItemName)
			if Money.Value >= Price and Player.Backpack:FindFirstChild(ItemName) == nil and Player.Character:FindFirstChild(ItemName) == nil and Item:FindFirstChild("GamepassID") == nil then
				Money.Value = Money.Value - Price
				local Clone = Item:Clone()
				Item.Parent = Player.Backpack
2 Likes

I assume this is a LocalScript, correct? And on which line are you getting the error Attempt to index nil with ‘FindFirstChild’?

7th and its a ServerScript i tried using RemoteEvents but this works fine only the FindFirstChild not._.

1 Like

Maybe you can split up the big if-statement into separate lines to help figure out which thing is nil. Item itself could be nil; if so, then the revised code shouldn’t error on that anymore since it’s being checked. If it’s Backpack or Character, then you should know from the line number that produces the error.

for i, button in pairs (script.Parent.ScrollingFrame:GetChildren()) do
	if button.Name == "TextButton" then
	button.MouseButton1Click:Connect(function()
			local Price = knop.Price.Value
			local ItemName = knop.ItemName.Value
			local Item = game.ServerStorage.Guns:FindFirstChild(ItemName)
			if Item then
				local PlayerKnopItem = Player.Backpack:FindFirstChild(ItemName)
				local CharacterKnopItem Player.Character:FindFirstChild(ItemName)
				local GamepassObject = Item:FindFirstChild("GamepassID")
				if Money.Value >= Price and PlayerKnopItem == nil and CharacterKnopItem == nil and GamepassObject == nil then
					Money.Value = Money.Value - Price
					local Clone = Item:Clone()
					Item.Parent = Player.Backpack

EDIT: @MilanK078 did you see this it might help you troubleshoot the problem at least

1 Like

In that line you have 3 things that could be throwing that error, Backback, Character, and Item, one of those doesn’t exist when you get the error.

Check if “Item” exists before referencing it in the logic.
(This is called Defensive Programming, if you wish to look it up)

FindFirstChild will return either the object you want, or NIL

If Item is NIL - then you cannot call
Item:FindFirstChild("GamepassID")

local Item = game.ServerStorage.Guns:FindFirstChild(ItemName)
if Item ~= nil and Item:FindFirstChild("GamepassID") == nil then

@MilanK078

1 Like

What is knop? Where is that located? It isnt mentioned anywhere else in the code that you have provided

Knop is button in Dutch its the TextButton that is pressed.

If something is erroring with :FindFirstChild(), it usually means that something is non existent or hasn’t been loaded yet. You could try to use :WaitForChild() instead, and if it spits out an infinite yield, said instance may not exist

But i want to know if something exists i can do it one time but not a second. If i don’t check it someone can have 2 the same tools. Sorry if I don’t explain it well, i 'm not good at explaining things._.

Its first searching for the item in the players backpack to don’t get the same item twice and then in the guns folder to check if its a gamepass only weapon.

What line is the error on?
(char limit)

The 7th line. i didn’t send the whole script btw only the part that matters.

Try printing the Item name and Player name to see if they are not nil like this

They are not nil. (character limit)

Thanks for all the help i did solve the problem :slight_smile: