Attempt to index nil with 'FindFirstChild'

So I followed this tycoon tutorial videos and now I’m stuck with this problem.

local tycoon = script.Parent.Parent

local mainItems = tycoon:FindFirstChild("MainItems")
local values = tycoon:FindFirstChild("Values")
local buttonsFolder = tycoon:FindFirstChild("Buttons")
local purchasedItems = tycoon:FindFirstChild("PurchasedItem")
local objectStorage = tycoon:FindFirstChild("PurchaseObjects") -- NEW!

local objects = {}

-- Claim tycoon ownership
mainItems.Ownerdoor.Door.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player and values.OwnerValue.Value == nil then
		local hasTycoon = player:FindFirstChild("HasTycoon")
		if hasTycoon and hasTycoon.Value == false then
			values.OwnerValue.Value = player
			hasTycoon.Value = true
			mainItems.Ownerdoor.Title.SurfaceGui.TextLabel.Text = player.Name .. "'s Tycoon"
		end
	end
end)

-- Function to set up a single button
local buttons = tycoon:FindFirstChild("Buttons")
local purchasedItems = tycoon:FindFirstChild("PurchasedItems")

local objects = {}

if buttons then
	for i, v in pairs(buttons:GetChildren()) do
		spawn(function()
			if v:FindFirstChild("Button") then

				local newObject = purchasedItems:FindFirstChild(v.Object.Value)
				if newObject ~= nil then
					objects[newObject.Name] = newObject:Clone()
					newObject:Destroy()
				else
					v:Destroy()
				end

				if v:FindFirstChild("Dependency") then
					v.Button.Transparency = 1
					v.Button.CanCollide = false
					v.Button.BillboardGui.Enabled = false
					coroutine.resume(coroutine.create(function()
						if purchasedItems:WaitForChild(v.Dependency.Value) then
							v.Button.Transparency = 0
							v.Button.CanCollide = true
							v.Button.BillboardGui.Enabled = true
						end
					end))
				end

				v.Button.Touched:Connect(function(hit)
					local player = game.Players:GetPlayerFromCharacter(hit.Parent)
					if player then
						if values.OwnerValue.Value == player then
							if v.Button.CanCollide == true then
								if player:FindFirstChild("leaderstats").Cash.Value >= v.Price.Value then
									player.leaderstats.Cash.Value -= v.Price.Value
									objects[v.Object.Value].Parent = purchasedItems
									v:Destroy()
								end
							end
						end
					end
				end)
			end
		end)
	end
end

Which findFirstChild is erroring out of the thousand

Which line is erroring?

Workspace.Tycoons.Tycoon.Scripts.Core:35: attempt to index nil with ‘FindFirstChild’

The error is saying that the value of the variable purchasedItems is nil, and hence

tycoon:FindFirstChild("PurchasedItems")

is resulting in nil.

Are you 100% sure that PurchasedItems is contained within the tycoon?

1 Like

The object in your screenshot is called PurchasedItem, while the script is looking for PurchasedItems. Change one of the values to match the other, and the script should work

(Also, I would suggest changing the code found in 25 and 26, and lines 3 through 7, to access the object using a dot (e.g. tycoon.MainItems) instead of doing `tycoon:FindFirstChild(“MainItems”). As the objects are required, it’s better that you get the error immediately rather than having to trace down the error later on)

2 Likes

Oh yeah you’re right! :sweat_smile:
Thank you for your suggestions.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.