Help with Luau syntax

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’s the before:

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.

Here’s the warning I get when I hover over the underline GoalCFrame in the lerp function.
image

FYI: In the future I may add replied to this post of other features that I’m stuck with underlines in.

Any help is greatly appreciated, thanks!

3 Likes

The underlined ones do not mind them. Its just a confusion by Roblox’s system thinking “GoalCFrame” is a boolean.

1 Like

Is there a way to make it not underlines? It’ll kind of be annoying and distracting to me. I hate it when working code has to be underlined…

1 Like

Yes!

local GoalCFrame
if Direction == "Open" then
    GoalCFrame = OpenCFrame
elseif Direction == "Close" then
    GoalCFrame = CloseCFrame
end
2 Likes
  1. 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.

  2. 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?

1 Like

One way to remove the Door: Instance and make it simply Door other is stay with the underlines that don’t cause harm.

just change the line with GoalCFrame to

local GoalCFrame = (Direction == "Open" and OpenCFrame) or (Direction == "Close" and CloseCFrame) or CFrame.new()

Your current GoalCFrame can be nil. You can’t Lerp to nil, so roblox shows a warning on it

local GoalCFrame: CFrame = ...

This also underlines it, but I think it underlines that entire line.

I fixed script for you

--!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

I also added types for the rest of variables

You could try local CFrame = ... :: CFrame not sure if that’ll help.

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)

Good news, this one also worked!

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()

Change the “game.ReplicatedStorage.CustonServices” to “game.ReplicatedStorage.CustomServices” because i accidantly typed it wrong

You can make it like that:

	local GoalCFrame = (Direction == "Open" and OpenCFrame) or CloseCFrame

So CloseCFrame will be default CFrame

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.

change GoalCFrame declaration to this:

local GoalCFrame: CFrame = ...

Or

local GoalCframe = Direction == "Open" and OpenCframe or CloseCframe

I think that should fix it. If not, just remove the --!strict and Roblox won’t highlight unnecessary warnings.

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.

Good.


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:

  1. 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)
  1. 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. :slight_smile:


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.