Nil Variable Error?

Hello, I’m writing a plugin that customizes your rig.

(I’m working on pant id inserter.)

However, I have an issue with this error:

20:07:57.231 - Script.lua:232: attempt to index nil with 'Changed'

And I tried inserting this code into the plugin script:

print(pantidbox == nil)

The Output Says:

true 
20:18:00.524 - Script.lua:232: attempt to index nil with 'Changed'

"true"is the output of print(pantidbox == nil)

I tried for fixing this issue but nothing worked. How can I fix this?

(“pantidbox” is a textBox type but it recognizes as nil.)

You’re not giving enough information, where are you calling the Changed function? What is pantidbox?

“pantidbox” is Basically a Text Box for I’m using the inserting pantTemplate id.

I’m Using the Changed for checking The TextBox’s Text Value:

pantidbox.Changed:Connect(getpantid(tonumber(pantidbox.Text)))

getpantid() function:

function getpantid(id)

local sucsess,name = pcall(function()
	market:GetProductInfo(id,Enum.InfoType.Asset)	
end)

if sucsess then
	pantidname.Text = name
	testpant.PantsTemplate = id
else
	
	pantidname.Text = "No Pants Selected"
end

end

and I’m Using this function to initilize the UI:

Initilizing Function
function startplugin()

frame2 = Instance.new("Frame",frame1)
frame2.Size = UDim2.new(1,0,0.95,0)
frame2.Position = UDim2.new(0,0,0.05,0)
frame2.BackgroundColor3 = Color3.fromRGB(200,200,200)
frame2.BorderSizePixel = 5

dressbutton = Instance.new("TextButton",frame1)
dressbutton.Size = UDim2.new(0.25,0,0.05,0)
dressbutton.Position = UDim2.new(0,0,0,0)
dressbutton.BackgroundColor3 = frame2.BackgroundColor3
dressbutton.Text = "Clothing"
dressbutton.BorderSizePixel = 0
dressbutton.TextScaled = true

viewf = Instance.new("ViewportFrame",frame2)
viewf.BackgroundColor3 = Color3.fromRGB(150,150,150)
viewf.Size = UDim2.new(0.4,0,0.9,0)
viewf.Position = UDim2.new(0.55,0,0.05,0)

if vfrig ~= nil then vfrig:Destroy() end
vfrig = rig:Clone()
vfrig.Parent = viewf

vfcam = Instance.new("Camera",viewf)
viewf.CurrentCamera = vfcam
local target = Vector3.new(4,4,4)
vfrig:SetPrimaryPartCFrame(CFrame.new(0,0,0))
vfcam.CFrame = CFrame.new(target,vfrig.PrimaryPart.Position)
viewf.ZIndex = 2
viewf.BackgroundTransparency = 1

platformimg = Instance.new("ImageLabel",frame2)
platformimg.Size = UDim2.new(0.4,0,0.9,0)
platformimg.Position = UDim2.new(0.55,0,0.05,0)
platformimg.Image = "rbxassetid://183400826"

rigdisp = Instance.new("TextLabel",frame2)
rigdisp.Size = UDim2.new(0.4,0,0.05,0)
rigdisp.Position = UDim2.new(0.05,0,0.05,0)
rigdisp.Text = vfrig.Name.."  RigType:"..(string.match(tostring(vfrig.Humanoid.RigType),"R6") or string.match(tostring(vfrig.Humanoid.RigType),"R15"))
rigdisp.TextScaled = true
rigdisp.BackgroundColor3 = frame2.BackgroundColor3
rigdisp.BorderSizePixel = 0

pantidname = Instance.new("TextLabel",frame2)
pantidname.Text = "No Pants Inserted"
pantidname.Size = UDim2.new(0.4,0,0.075,0)
pantidname.BackgroundColor3 = Color3.fromRGB(175,175,175)
pantidname.Position = UDim2.new(0.05,0,0.2,0)
pantidname.TextScaled = true
pantidname.BorderSizePixel = 0

pantidbox = Instance.new("TextBox",frame2)
pantidbox.PlaceholderText = "Insert ID..."
pantidbox.Text = ""
pantidbox.Size = UDim2.new(0.4,0,0.05,0)
pantidbox.BackgroundColor3 = Color3.fromRGB(150,150,150)
pantidbox.Position = UDim2.new(0.05,0,0.275,0)
pantidbox.TextScaled = true
pantidbox.BorderSizePixel = 0

if not vfrig:FindFirstChildOfClass("Pants") then	
	testpant = Instance.new("Pants",vfrig)
	testpant.Name = "Pants"
end


    end

btw Everything shows up correctly,But the plugin recognizes “pantidbox” is a nil value.

RBXScriptSignal:Connect requires a function to call when the signal is fired, however you’re already calling the function inside.

You could get around this by doing:

pantidbox.Changed:Connect(function()
    getpantid(tonumber(pantidbox.Text))
end)

I tried, but nothing worked. I said:

(“pantidbox” is a textBox type but it recognizes as nil.)

I’m calling a “pantidbox” in this form via Instance.new():

pantidbox = Instance.new("TextBox",frame2)
pantidbox.PlaceholderText = "Insert ID..."
pantidbox.Text = ""
pantidbox.Size = UDim2.new(0.4,0,0.05,0)
pantidbox.BackgroundColor3 = Color3.fromRGB(150,150,150)
pantidbox.Position = UDim2.new(0.05,0,0.275,0)
pantidbox.TextScaled = true
pantidbox.BorderSizePixel = 0

and I’m using “pantidbox.Changed:(…)” command after this function:

function getRigs()
local selected = selection:Get()
local models = {}


if #selected > 0 then
	for v,i in ipairs(selected) do
		if selected[v].ClassName == "Model"	then		
			models[v] = selected[v]
			
		end		
	end
	
	
else	
errortext.Text = "No Rig Selected"
error("Plugin cannot find a Model or Model didn't selected.")		
end

	if models[1]:FindFirstChildOfClass("Humanoid") then
	
	  rig = models[1]
	found = true
	errortext.Visible = false
	retrybtn.Visible = false
	startplugin()
	history:SetWaypoint("Rig Filtered.")
	end	


end

into here:

initdock()

print(pantidbox == nil)

button1.Click:Connect(function()
    initdock()
    errortext.Visible = true
	retrybtn.Visible = true
    
	dockFrame.Enabled = not dockFrame.Enabled
	
	getRigs()
   	refreshColors()

    
    
end)


settings().Studio.ThemeChanged:Connect(refreshColors)

pantidbox.Changed:Connect(function()
   getpantid(tonumber(pantidbox.Text))
end)

startpugin() initilizes the gui includes the “pantidbox” instance

Maybe I shoud call “startplugin()” after “getrigs()” function?

Well since pantidbox is a global, the startplugin function needs to be higher than the Changed & it needs to be ran before the Changed.

I’d recommend against the use of global variables like this though.

Yeah I fixed it like this:

button1.Click:Connect(function()
    initdock()
    errortext.Visible = true
	retrybtn.Visible = true
    
	dockFrame.Enabled = not dockFrame.Enabled
	
	getRigs()
	
	if rig ~= nil then
		startplugin()
	end
	
   	refreshColors()
	
    pantidbox.Changed:Connect(function()
	getpantid(tonumber(pantidbox.Text))
	end)
    
end)

Thanks for helping! @return_end1

1 Like