BillB1ox
(Will)
July 19, 2023, 2:29am
#1
Greetings! I am a Swift Developer, and Force Unwrapping is when you get an error like this:
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
BillB1ox
(Will)
July 19, 2023, 2:33am
#3
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
BillB1ox
(Will)
July 19, 2023, 2:35am
#5
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
1 Like
BillB1ox
(Will)
July 19, 2023, 2:37am
#7
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
BillB1ox
(Will)
July 19, 2023, 2:42am
#10
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
Mystxry12
(Mystxry)
July 19, 2023, 9:49am
#13
Unfortunately Roblox doesn’t have fancy features like adding a !
to silence type errors . But afaik there are some ways to do this:
Out of all I would probably choose the first two.
2 Likes