Luau Type Checking

Simple request here:

local Damage = script.doDamage:Clone()

Damage.Parent = fireBall
Damage.Disabled = false

image

doDamage is a script that I clone into a model and then enable. How do I silence this little warning without adding

--!nocheck

To the beginning of the script?

You could disable the script manually, not by the script.

This is because type checker gets angry because :Clone() returns an Instance and since the .Disabled property isn’t present in the Instance superclass, you have to use the : any type to silence it.

2 Likes

I’m so confused, is this luau? I’ve never seen anyone use a colon in a variable before

Yep, it’s type checking. It’s just a neat tool to help people if they don’t see anything wrong with their code and saves time if Luau thinks it’s likely to error.

1 Like

Thank you, didn’t know :Clone() didn’t return the same superclass as the object it cloned. : any worked.

1 Like

I think it’s because :Clone is a function of Instance which means it would return type Instance whereas the .Disabled property is from BaseScript and since :Clone returns an Instance which isn’t always a base script, that’s why it happens. I could be wrong but that’s my guess.

That seems like a good explanation. I did a little digging earlier while trying to solve this and tried typing it as everything that Script inherits from. None worked.

2 Likes

In my opinion, a cleaner approach that means you still get to keep your type would be to use type assertion:

--!strict
local coolInstance = script.Parent.Baseplate:Clone() :: Part
coolInstance.Color = Color3.fromRGB(189, 255, 153)
5 Likes