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!