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

What does "attempt to index nil with 'FindFirstChild'" mean?

I am making a tycoon with GamerM8' videos, and I am pretty sure I have copied the script word for word, I have messed around with dependency and item StringValues and it still comes up with an error: "Workspace.Tycoon Model.Scripts.MainScript:27: attempt to index nil with 'FindFirstChild'"

Code:

--- Buying Functions ---

for i,v in pairs(Buttons:GetChildren()) do local NewItem = BoughtItems:FindFirstChild(v.Item.Value) if NewItem ~= nil then Items[NewItem.Name] = NewItem:Clone() NewItem:Destroy() else v.ButtonPart.Transparency = 1 v.ButtonPart.CanCollide = false end

if v:FindFirstChild("Dependency") then
    coroutine.resume(coroutine.create(function()
        v.ButtonPart.Transparency = 1
        v.ButtonPart.CanCollide = false
        if BoughtItems:WaitForChild(v.Dependency.Value, 100000) then
            v.ButtonPart.Transparency = 0
            v.ButtonPart.CanCollide = true
        end
    end))
end

v.ButtonPart.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Values.OwnerValue.Value == Player then
            if v.ButtonPart.CanCollide == true and v.ButtonPart.Transparency == 0 then
                if Player:WaitForChild("leaderstats").Cash.Value >= v.Price.Value then
                    Player.leaderstats.Cash.Value -= v.Price.Value
                    Items[v.Item.Value].Parent = BoughtItems
                    v:Destroy()
                end
            end
        end
    end
end)

end

the line in question would be: local NewItem = BoughtItems:FindFirstChild(v.Item.Value)

1 answer

Hello, if thats whole code, you've got this problem. You did not define "BoughtItems" variable in earlier code. So that variable is nil, because it does not exist. I recommend you to never copy scripts, and script them yourself, because chance of erroring if you change the code is very high after, and you don't even know fully what code does do. Next time please send all your code formated in code block, because you got code inside of codeblock and outside too. It is confusing. I wish this will help and I wish you good luck in future coding!

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