Find methods can return nil so you ideally should add a question mark; this will get rid of the underline for your Humanoid declaration but one will appear under Died because nil has no properties, therefore both Humanoid.Died and nil.Died can’t both be satisfied.
If you check the existence of a Humanoid, underline goes. You should be doing this with or without typechecking to begin with rather than assuming that a find method returns a non-nil value.
if Humanoid then
-- Your Died connection goes here
end
I’m used to using WaitForChild for things like this, so I totally forgot about waiting for the object to load
in. I changed the code while messing with it, and forgot to change it back.
This code seems to satisfy the linter
--!strict
game.Players.PlayerAdded:Connect(function(AddedPlayer: Player)
AddedPlayer.CharacterAdded:Connect(function(NewChar: Model)
NewChar:WaitForChild("Humanoid")
local Humanoid: Humanoid | nil = NewChar:FindFirstChildOfClass("Humanoid")
if Humanoid then
Humanoid.Died:Connect(function()
AddedPlayer:LoadCharacter()
end)
end
end)
AddedPlayer:LoadCharacter()
end)
Also, by the way, to make it even cleaner, the type annotation on the variable is unnecessary since you’re already asserting that it’s a Humanoid via the type assertion operator ::
No. There is a difference between local x: y = z and local x = z :: y. In some cases you’ll want to leave it to type inference (e.g local x = 2 – Luau can already infer that the value on the right hand side is a number). In this case you’re already asserting the type of the value therefore unnecessary to add a type annotation.
On second thought, I probably can’t predict the syntax changes if Luau becomes more of a compiled language. You’re right, there’s no real practical difference.