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

Why am I getting an error saying my function is nil?

I've been looking all over this script but I can't find why its saying my update() function is nil... This question is mostly just about the first "if then" statement and the update() function but I included the whole script to give better context. Let me know if you have any questions, and thanks for any help!

local BUTTON = script.Parent
local player = game.Players.LocalPlayer
local MFRAME = game.Players.LocalPlayer.PlayerGui.BackpackGui.MFrame
local IFRAME = game.Players.LocalPlayer.PlayerGui.BackpackGui.MFrame.IFrame
local OPEN = game.Players.LocalPlayer.PlayerGui.BackpackGui.Backpack:FindFirstChild("Open")
local mouse=player:GetMouse()
local RANGE = 15
------------------------------
wait(3)

local CURRENT = nil

local RANGE = 15

MFRAME.Visible = false
-------------------------------
if OPEN ~= nil then 
	 update() print("yesss")
end


function removeExample()
	local player = game.Players.LocalPlayer
	local char = player.Character

	local ex_check = char:findFirstChild("BACKPACK_EXAMPLE")

	if ex_check ~= nil then
		ex_check:remove()
	end
end

function rangeCheck(mouse)
	local player = game.Players.LocalPlayer
	local char = player.Character
	local torso = char.Torso

	return (torso.Position - mouse.hit.p).magnitude < RANGE
end

function do_remove_all(parent)
	local children = parent:GetChildren()

	for index, child in pairs(children) do
		child:remove()
	end
end



function onGuiClick(part)
	CURRENT = part
	update()
end


function update()
	local player = game.Players.LocalPlayer
	local char = player.Character
	local pack = player:findFirstChild("Pack")
	local pack2 = player:findFirstChild("Pack2")
	local pack_tab = pack:GetChildren()
	local Ipack_tab = IFRAME:GetChildren()

	if CURRENT == nil then print ("current is nil")
		local x = 0
		local y = 0
	
		for index, child in pairs(pack_tab) do
			local CHOICE_GUI = MFRAME.TextButton:clone()
CHOICE_GUI.Size = UDim2.new(0, 100, 0, 20)
CHOICE_GUI.Transparency = .4
CHOICE_GUI.BackgroundColor3 = Color3.new()
			local new_gui = CHOICE_GUI 
			CURRENT = new_gui
			new_gui.Position = UDim2.new(0, x, 0, y)
			new_gui.Text = child.Name
			new_gui.TextColor3 = Color3.new(255,255,255)
			new_gui.Name = child.Name
			new_gui.MouseButton1Down:connect(function() onGuiClick(child) end)
			new_gui.Parent = IFRAME
			new_gui.Visible = true
			local CHILD = pack:FindFirstChild(child.Name)
			CHILD.Parent = pack2
			
			y = y + 20
			
			if y > 240 then
				y = 0
				x = x + 100
				
			end
			
		end
			
		--cleanGui()
	end
	end

function do_remove_all(parent)
	local children = parent:GetChildren()

	for index, child in pairs(children) do
		child:remove()
	end
end

function rangeCheck(mouse)
	local char = player.Character
	local torso = char.Torso

	return (torso.Position - mouse.hit.p).magnitude < RANGE
end

function onMouseButton1Down()
	local pack2 = player:findFirstChild("Pack2")
local pack_tab = pack2:GetChildren()
local partname = BUTTON.Text
local part = pack2:FindFirstChild(partname)
	local example = part:Clone()
	local char = player.Character
	MFRAME.Visible = false
		
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
	mouse.Move:connect(function() onMouseMove(mouse) end)
	mouse.KeyDown:connect(function(key) onKeyDown(key, mouse) end)

end

function removeExample()
	local player = game.Players.LocalPlayer
	local char = player.Character

	local ex_check = char:findFirstChild("BACKPACK_EXAMPLE")

	if ex_check ~= nil then
		ex_check:remove()
	end
end


function onMouseMove(mouse)
	local player = game.Players.LocalPlayer
	local char = player.Character
	local torso = char.Torso
	local pack2 = player:findFirstChild("Pack2")
local pack_tab = pack2:GetChildren()
local partname = BUTTON.Text
local part = pack2:FindFirstChild(partname)

local CURRENT = part
	removeExample()
	

	if CURRENT ~= nil then
		if rangeCheck(mouse) then
			
			local example = CURRENT:clone()
			example.Name = "BACKPACK_EXAMPLE"
			example.Locked = true
			example.Transparency = 0.5
			example.Parent = char
			example.Position = mouse.hit.p
			do_remove_all(example)
			
		end
	end
end

function onButton1Down(mouse)
	local pack2 = player:findFirstChild("Pack2")
local pack_tab = pack2:GetChildren()
local partname = BUTTON.Text
local part = pack2:FindFirstChild(partname)
local player = game.Players.LocalPlayer
local char = player.Character
local example = char:FindFirstChild("example")
local CURRENT = part

	MFRAME.Visible = true
	
	if CURRENT ~= nil then
		if rangeCheck(mouse) then
			removeExample()
			print("clicked")

			CURRENT.Parent = game.Workspace
			CURRENT.Locked = false
			CURRENT.Anchored = false
			CURRENT.Position = mouse.hit.p
			CURRENT = nil
			--do_remove_all(example)
			--cleanGui()
			script.Parent.Visible = false
			script.Parent:Destroy() print("buttonremoved")
			update() print("primaryupdate")
			
			
	end
	end
	end

function onKeyDown(key, mouse)
	if key == "r" then
		removeExample()
		local CURRENT = nil
	elseif key == "e" then
		local player = game.Players.LocalPlayer
		local pack = player:findFirstChild("Pack")
		local pack_tab = pack:GetChildren()

		for index, child in pairs(pack_tab) do
			local CURRENT = child
			onButton1Down(mouse)
			wait(0.1)
		end

		local CURRENT = nil
	end
end


BUTTON.MouseButton1Down:connect(onMouseButton1Down)

1 answer

✓ Accepted answer

You don't appear to define update before calling it on line 18. Lua doesn't support function prototyping in the same way as other languages, largely because it is interpreted.

2 comments

1

Meaning: the code is read top-to-bottom, so you'll have to move the function definition to the top, before all the calls to it.

0

awesome, thank you! kind of stupid how they let the blue underline go away in this situation huh? they should add a yellow underline for when this happens

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