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

Attempted to index a nil value error help?

I have the script:

script.Parent.MouseButton1Click:Connect(function()
	local JobPanel
	if not exists then
	exists = true
	JobPanel = Instance.new("Frame", script.Parent.Parent.Parent.Parent)
	JobPanel.BackgroundTransparency = 1
	JobPanel.Name = "Job Panel"
	JobPanel.AnchorPoint = Vector2.new(0.5, 0.5)
	JobPanel.Position = UDim2.new(0.5,0,0.5,0)
	JobPanel.Size = UDim2.new(0,800,0,450)
	local Title = Instance.new("TextLabel", JobPanel)
	Title.BackgroundColor3 = Color3.new(255,255,255)
	Title.Size = UDim2.new(0,750,0,30)
	Title.Text = "Job Menu"
	Title.TextScaled = true
	else
	JobPanel:Destroy()
	end
end)

But the Destroy() doesn't work because for some reason JobPanel is nil? Help?

Error:

Players.radusavin366.PlayerGui.HUD.MainHUD.Buttons.JobPopup.LocalScript:19: attempt to index local 'JobPanel' (a nil value)
14:44:53.855 - Stack Begin
14:44:53.855 - Script 'Players.radusavin366.PlayerGui.HUD.MainHUD.Buttons.JobPopup.LocalScript', Line 19
14:44:53.856 - Stack End

1 comment

0

In line 2, you didnt mention what is "local JobPanel". Add "=" after the JobPanel and tell the script what is JobPanel.

1 answer

✓ Accepted answer

Ok, I'm not exactly sure what the problem is but next time show us your variables as well.

Ok the problem may be through the Instance.new(). What you want to do is instead of `instance.new('asadas',script.parent) you want to set the parent on another line (again for some reason it's probably this).

local Food = Instance.new('TextLabel')
Food.Parent = script.Parent.Parent.Parent.Parent

So your script would look like:

script.Parent.MouseButton1Click:Connect(function()
    if not exists then
    	exists = true
    	local JobPanel = Instance.new("Frame")
	JopPanel.Parent = script.Parent.Parent.Parent.Parent
    	JobPanel.BackgroundTransparency = 1
    	JobPanel.Name = "Job Panel"
	JobPanel.AnchorPoint = Vector2.new(0.5, 0.5)
    	JobPanel.Position = UDim2.new(0.5,0,0.5,0)
   	JobPanel.Size = UDim2.new(0,800,0,450)
    	local Title = Instance.new("TextLabel")
	Title.Parent = JobPanel
    	Title.BackgroundColor3 = Color3.new(255,255,255)
    	Title.Size = UDim2.new(0,750,0,30)
    	Title.Text = "Job Menu"
    	Title.TextScaled = true
    else
    	JobPanel:Destroy()
    end
end)

Now I'm not exactly sure exists actually is a variable that is false make sure it is.

Paste this code in and tell me if it errors, if it does I will get back to you. Comment below and tell me the error on which line.

Hoped this helped!

2 comments

0

Yeah the exists bool actually exists, I didn't copy it in. Thanks for helping, although i fixed it by making a button before the else that destroys JobPanel.

0

Then you would probably need a wait() lol.

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