No errors in the module

Hello developers,

I have a module that controlling my game’s door system and I have found something weird.

The module should error because my code is indexing nil with FindFirstChild() method but it just does nothing as unexpected.

I can’t find why it doesn’t error so I created the topic.

Here is code I mentioned:

-- Run function
function DoorHandler:Run(SpawnData: {CurrentRoomNumber: number, Object: Model, Room: Model}): ()
	local CurrentRoomNumber = SpawnData.CurrentRoomNumber
	local RunningDoor = SpawnData.Object
	local Room = SpawnData.Room
	local PrimaryPart = RunningDoor.PrimaryPart :: BasePart
	local Hitbox = RunningDoor:FindFirstChild("Hitbox") :: BasePart
	local NextRoomName = tostring(CurrentRoomNumber)
	local NextRoom = Rooms:FindFirstChild(NextRoomName)
	local Spawns = NextRoom:FindFirstChild("Spawns") :: Instance
	local RoomNumberSign = RunningDoor:FindFirstChild("RoomNumberSign") :: BasePart
	local SurfaceGui = RoomNumberSign:FindFirstChild("SurfaceGui")
	local TextLabel = SurfaceGui:FindFirstChild("TextLabel") :: TextLabel
	TextLabel.Text = NextRoomName
	DoorHandler:WaitForTouching(Hitbox)
	RoomController.SpawnObjects(NextRoom, Spawns)
	PrimaryPart.CanCollide = false
	PrimaryPart.Transparency = 1
	RoomNumberSign.Transparency = 1
	TextLabel.TextTransparency = 1
	RoomController:GenerateRooms(1)
end

What I feel weird is that it causes error if I make a code to make error outside of the function, it works.

But only in the function, it doesn’t.

There isn’t any functions like pcall() in the script that runs the function.

Here is a code which runs the function:

-- Spawn something in a certain room.
local function Spawn(Object: BasePart, CloneRoom: Model): ()
	local BaseName = "Controller"
	local ObjectType = string.split(Object.Name, "Spawn")[1]
	local Template = ObjectsTemplates:FindFirstChild(ObjectType) :: Model
	local CloneTemplate = Template:Clone()
	CloneTemplate:PivotTo(Object.CFrame)
	CloneTemplate.Parent = CloneRoom
	local ObjectName = ObjectType..BaseName
	local ObjectModuleScript = ObjectsController:FindFirstChild(ObjectName) :: Instance
	if not ObjectModuleScript then
		DebugHandler:Error(script.Name, `{ObjectName} module not found!`)
	end
	local ObjectModule = require(ObjectModuleScript) :: ObjectModule
	local SpawnData = {
		CurrentRoomNumber = CurrentRoomNumber,
		Object = CloneTemplate,
		Room = CloneRoom
	}
	local NewObjectThread = coroutine.create(function()
		ObjectModule:Run(SpawnData)
	end)
	local RunningObjectFunction = RunningObjectFunctions[CloneRoom]
	if not RunningObjectFunction then
		RunningObjectFunctions[CloneRoom] = {}
	end
	coroutine.resume(NewObjectThread)
	table.insert(RunningObjectFunctions[CloneRoom], {CloneTemplate, NewObjectThread} :: {Model | thread})
end

Any helps would be appreciated!

could you point out which FindFirstChild call is your concern?

1 Like

This line; I printed NextRoom and it printed nil. So I expected the module errors what I said, but I didn’t find any errors in the output.

local NextRoom = Rooms:FindFirstChild(NextRoomName)
shouldnt this error because theres no Rooms, it should be Room?

Rooms is a folder that which will be each Room’s parent. So it’s a correct code.

The error “Attempt to index nil with [something]” only happens when the method or property [something] does not exist on a table or instance. FindFirstChild returns nil if it does not find the part with the same name, however, if you try to use FindFirstChild on a variable that is nil, it WILL error.

Here’s an example:

-- in workspace, there is a folder named "CoolFolder".
-- in it, there is a descendant named "CoolDescendant".
-- here's how FindFirstChild will behave.
-- CoolDescendant does NOT have a child named "IDoNotExist"

local coolFolder = workspace:FindFirstChild("CoolFolder") -- returns the folder
local coolDesc = coolFolder:FindFirstChild("CoolDescendant") -- returns the descendant
local nonExisting = coolDesc:FindFirstChild("IDoNotExist") -- returns nil
local thisWillError = nonExisting:FindFirstChild("GonnaError") -- errors, because you are trying to do
--nil:FindFirstChild()

hope this helps.

I’m afraid the topic is difficult to understand, what I meant was not it has error, but it doesn’t make error.

Did you print NextRoom within the method where you set Spawns?

Interesting. Can you include the code with the print code?

Coroutines hide errors btw, i keep forgetting this and it bites me in the back. I think the task library doesn’t hide errors so I use that where possible.

You were right. But is it okay to just let it do? I think someone should report it.

It is intentional and documented behaviour. Not sure why. I think coroutine.resume actually returns the error message if there is an error.

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