How do I force unwrap in LuaU?

Greetings! I am a Swift Developer, and Force Unwrapping is when you get an error like this:
image

Force Unwrapping would allow you to not have this error.
Basically, I want to say to my code “I know it is going to work properly, just do it.”

In Swift, to force unwrap, you would do FeaturedPlayer.Team!.Name

I tried adding an !, but it red underlined the !.

How do I force unwrap in LuaU without setting the script to --!nocheck

Thank you!

3 Likes

maybe you an just check if the team is nil before doing wtv

if FeaturedPlayer.Team ~= nil then

end
1 Like

While it does work, I don’t want to do this every single time, especially if I design the game to not have it nil. Also makes code not clean tbh. Is there no way to force unwrap in LuaU?

1 Like

(idk what force unwrapping it, but as you explained it)

no, not really

and if the players team cannot be nil, i don’t think it would even matter

1 Like

It’s basically just bypassing the possible error without needing to add another line if you know for sure if it’s irrelevant for you, which in my case it is.

Player.Team is considered optional.

The ! in swift does this.

1 Like

yeah no, you would probs just add the extra code :confused:

1 Like

Lol this makes me wish Swift was the default language of Roblox even more…

1 Like

and i guess its only a warning though, its just letting you know

!!be careful!!
omg!! the team can be nil!!

no but really, its probably for new/unexperienced devs to let hem know that it can error

2 Likes

Look very ugly, but you can do this:

(FeaturedPlayer.Team::Team).Name
1 Like

Yeah it does… Lol LuaU is no where as good as Swift… Unless we have force unwrapping…

1 Like

lol
(this dev forum chars limit is so annoying)

2 Likes

Cast it to any or the type you want, kills typechecking depending on context however

(FeaturedPlayer.Team :: Team).Name

1 Like

Unfortunately Roblox doesn’t have fancy features like adding a ! to silence type errors :sob:. But afaik there are some ways to do this:

  • Cast it to a type:

    • (Player.Team :: Team).Name = ""
  • Doing a check beforehand:

    • if not Player.Team then return end or if Player.Team then --code end
  • Disabling type for player; basically don’t do Player: Player

Out of all I would probably choose the first two.

2 Likes