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
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
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.