Make API functions throw if the params aren't the correct type

Did you know that game.Workspace:FindFirstChild(7) just returns nil? It doesn’t throw even though it expects a string as an argument. You can pass anything to FindFirstChild (except nil) and it doesn’t throw. Probably many of our API calls are like this. Another legal call is game:IsA(7) or even game:IsA({})

When you are building objects out of tables it is very easy to send the wrong type or field into a function because there is no type checking.

This bug should have raised a runtime error and instead I spent 5 minutes staring at it trying to figure out what is going on.
image

8 Likes

When passing a number, it gets implicitly converted to a string. This is also done by many library functions, and is hard to change because of backwards compatibility. workspace[7] works the same as workspace:FindFirstChild(7), it searches for a child named tostring(7) (which is probably "7", although this isn’t guarnateed).

Some of these functions accept weird values, for instance workspace:FindFirstChild{} is the same as workspace:FindFirstChild"{}"

I tried passing values of these types: Axes, BrickColor, CatalogSearchParams, Color3, ColorSequence, DateTime, DockWidgetPluginGuiInfo, Enum, Enums, Faces, FloatCurveKey, NumberRange, NumberSequence, NumberSequenceKeypoint, OverlapParams, PathWaypoint, PhysicalProperties, RBXScriptConnection, RBXScriptSignal, Random, Ray, RaycastResult, Rect, Region3, Region3int16, RotationCurveKey, TweenInfo, UDim, UDim2, Vector2, Vector2int16, Vector3int16, CellId, PluginDrag, userdata created by newproxy, and coroutine. They all generated errors when passing them as the second argument to FindFirstChild, e.g. print((pcall(workspace.FindFirstChild,workspace,Vector2.new()))) prints false and workspace:FindFirstChild(Vector2.new()) errors.

It’d be better if passing CFrame, Vector3, RaycastParams, table, function, and boolean values caused errors. These values are likely seldom passed, and if they are passed are then it is probably a bug. There is a substantial amount of code that relies on passing a number (e.g. doing FindFirstChild with an index), making this error would be a breaking change and would be inconsistent with many library functions (e.g. string.rep(1,10) and math.abs"-1" both work).

9 Likes