Hey there!
So currently I’m trying to learn a bit of Luau but its kind of confusing to me but, I’m working on train system and I’m trying to make it compatible with Luau, but I’m getting underlines on some parts of the code when I strict it. How could I fix these? Here’s one attempt from some one part of the code.
Here is an attempt:
I’ve managed to get rid of the underlines for a few lines, however I’m stuck with the underlines on the GoalCFrame part and I’m unsure how to fix it.
Is there any other ways or just this way? I did think of it but, wasn’t sure if there’s a more better way or not.
With my little Door: Instance & {Motor1: Motor6D, Motor2: Motor6D} trick, would this be the correct way to get rid of the underlines from the first picture or is there a better way?
--!strict
local ThreadService = require(game.ReplicatedStorage.CustonServices.ThreadService)
local CustomService = {}
local CloseCFrame: CFrame = CFrame.new(0, 0,0)
local OpenCFrame: CFrame = CFrame.new(0, 0, 2.929)
function CustomService:AnimateDoor(Direction: string, Door: Instance& {Motor1:Motor6D, Motor2: Motor6D})
local GoalCFrame: CFrame = (Direction == "Open" and OpenCFrame) or (Direction == "Close" and CloseCFrame) or CFrame.new()
local StartTime: number = tick()
ThreadService.new(Door, 0, function(Tick: number)
local ElapsedTime = Tick - StartTime
local Time = ElapsedTime / 8
if Door.Motor2.C1 == GoalCFrame then
ThreadService:Destroy(Door)
else
Door.Motor1.C1=Door.Motor1.C1: Lerp(GoalCFrame, Time)
Door.Motor2.C1=Door.Motor2.C1: Lerp(GoalCFrame, Time)
end
end)
end
Thanks! This worked. I just changed the CFrame.new to the ClosedCFrame so its more performant (I think its a tiny bit more performant reusing CFrames instead of creating new ones)
Not sure what is better to use though.
local CFrame = … :: CFrame or adding the extra or using local GoalCFrame: CFrame = (Direction == “Open” and OpenCFrame) or (Direction == “Close” and CloseCFrame) or CFrame.new()
I’ve just closed studio down for the night because I’m going to bed now but, I’ll update you tomorrow how I get on. I might have some more functions to get looked at as well because I might have underline issues with other parts of the script.
The first line of code didn’t work when I first tried it but the second line of code did.
Question: Is Roblox coding already Luau without strict or does using strict make it luau? Or what exactly does strict do? I think it means it just like underlines it so you can ensure it’s all luau and whatever but I’ll double check with someone.
No, not like that. The --!strict is one of the 3 “flags” available in Luau, the 3 are:
--!nocheck,
--!nonstrict (default), and
--!strict
I would explain the differences but I think the luau docs would explain them better, you should read this and it will hopefully make sense, if not, feel free to ask me any questions you want.
You got plenty of answers to suppress the warning, but here’s two more to consider:
AnimateDoor only supports two values passed in for Direction: “Open” and “Close”. So you can just use the type system to limit the values.
function CustomService:AnimateDoor(Direction: "Open"|"Close", Door: Instance)
GoalCFrame can be false if your Direction string is not one of the two expected (as several have pointed out). One other way to do this ternary operation (which I’ve found often plays nicer with the type system) is as follows:
local GoalCFrame = if Direction == "Open" then OpenCFrame else CloseCFrame
It’s a Luau extension that I think the devs added as a bid to get us to stop writing confusing short-circuit evaluation expressions.
I would take solution #1, since I think tightening my types is helpful. It will mean, though, that you can’t pass a string type to this function – it must be a “Open” or “Close” or “Open”|“Close” type. If you’re hardcoding constants, then there’s no issue. Otherwise, you may have to do explicit type narrowing.