OOP, attempt to call a nil value

local function CreateRigFunction()
	if RigObject then	-- A rig had already been created
		print(RigObject)
		RigObject:DestroyRig()
	end
	RigObject = RigControllerModule.new()
end

print(RigObject) does print out the already created object. However, I still get this error, and the method DestroyRig() isn’t executed.

3 Likes

Is DestroyRig() a defined function within the same script or are you getting confused with the built-in “Destroy()” function?

DestroyRig() is a method in my modulescript.

Just checking, thanks for clarifying, do you get any console errors when running the game in studio?

Please read the title and my OP.

Right, but can you share the exact error message displayed in console? That would be useful since it typically gives information such as the line in the script at which the error occurred etc.

The error is thrown on the line RigObject:DestroyRig()

Attempt to call a nil value would otherwise indicate that you’re attempting to call a nil type value as a function. Meaning that the function “DestroyRig” either doesn’t exist or cannot be reached within the body of the function named “CreateRigFunction”.

RigObject:DestroyRig()


Try below instead


RigObject:Destroy()

This was my initial suggestion, but he/she asserted that a function named “DestroyRig” exists.

@amadeupworld2


Would perfer you try out the “Destroy()” method instead of “DestroyRig()” because I don’t think that’ll work. If “DestroyRig()” even exist, that is.

The error would suggest that the function likely doesn’t exist, that or it wasn’t correctly referenced from the module script. Or the function cannot be accessed from within the scope of the local function named “CreateRigFunction”.

If you read the title, I specifically mention I’m using OOP to manage my rig system. DestroyRig() is a method in my module script.

I’m not use to this type of coding because the functions above but what I do recommend is instead of
RigObject:DestroyRig()
do
RigObject
DestroyRig()

if that does not work idk

It’s not being recognized in the main script, as the error suggests when attempting to call “DestroyRig”, Lua thinks you’re trying to call a nil value as a function.

Solved the problem, I had forgot to set the tables index to itself in my module script.

1 Like