Attempt to call a nil value that isn't nil

Everytime the codes run for some reason output says that “button” is nil even tho it’s not

local function LoadData()
	task.wait(0.4)
	for i, v in pairs(Farm.Bought:GetChildren()) do
		for x, button in pairs(Buttons:GetChildren()) do
			if v.Name == button.Name then
				if v.Value == true then
					print(button)
					LoadButtons(button) --Error Here (Line 67)-- 
				end
			end
		end
	end
end

Output:

Use a check. It may because it quite literally doesn’t exist.

local function LoadData()
	task.wait(0.4)
	if Buttons then
		for i, v in pairs(Farm.Bought:GetChildren()) do
			for x, button in pairs(Buttons:GetChildren()) do
				if v.Name == button.Name then
					if v.Value == true then
						print(button)
						LoadButtons(button)
					end
				end
			end
		end
	else
		warn("Buttons object not found!")
	end
end

if its not because of that then show me the LoadButtons function.
Also make sure a button actually exists too.

It’s not because of that. Everything works but not the loadbuttons function.

LoadButtons Function:

function LoadButtons(btn)
	if btn:FindFirstChild("CropsToClone") then
		local cropstoclone = btn:FindFirstChild("CropsToClone").Value:Clone()
		for i, place in pairs(Farm:GetChildren()) do
			if place.Name == "Regrow" then
				place:Destroy()
			end
	    end
		cropstoclone:FindFirstChild("Regrow").Parent = Farm
		btn:Destroy()	
	else
		local itemtogive = btn:FindFirstChild("ItemToGive").Value:Clone()
		for i, tool in pairs(Player.Backpack:GetChildren()) do
			if tool:IsA("Tool") then
				tool:Destroy()
			end
		end
		itemtogive.Parent = Player.Backpack
		btn:Destroy()
	end
end

You are sure that a button exists?
If not then it seems to me that CropsToClone or ItemToGive may not exist.

I’m sure it exists because before calling a function I have print that checks if it exists.

Maybe you are right. Maybe CropsToClone or ItemToGive isn’t getting read by the script. I will try to check that and I will tell you if you’re right

CropsToClone and ItemToGive does exist.

I don’t know what can be the problem here because everything works perfectly fine. I’ve tried to make it so the function will call only if the Button isn’t nil but it didn’t work

What does Button print?
|||||||||

It prints a part That is Named UpgSickle1

use WaitForChild not FindFirstChild

Still doesn’t work for some reason

Does it give you the same error?

Yep, The same error in the same line

It looks like Button is the issue. Im not sure I can help further

1 Like

Still Thanks for help. I will try to figure it out later.

I fixed it. The problem was that the function was being defined after stepping on a button but I called it Before it got defined

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