How could i shorten/optimize this line?

Hi! I would want to know how i could optimize this line of code:

if previousNode.Name == "0" then pathLenght = (previousNode.CFrame - node.Start.CFrame) else pathLenght = (previousNode.End.CFrame - node.Start.CFrame).Magnitude end

Basically, i need to do this “if” statement because if the previous node doesn’t have any end to it, which is exclusively the beginning node, then the previousNode.End.CFrame bugs out because there is no end in the previousNode. Is there any way to optimize it so that it doesnt use an if statement? Let me know if you dont understand cuz im kinda mid at english.

This is as short as you should try to make it. Anything shorter will make less sense.

Ok, so there’s no way to avoid an if statement there?

I mean anything you replace it with will be doing the job of an if statement but less clearly.

One approach to optimize this code is by using the ternary operator, which allows you to conditionally assign a value in a more concise way.

local pathLength = (previousNode.Name == "0") and (previousNode.CFrame - node.Start.CFrame) or (previousNode.End.CFrame - node.Start.CFrame).Magnitude

This code checks if the previousNode.Name is “0”. If it is, it calculates the path length as (previousNode.CFrame - node.Start.CFrame) , otherwise, it calculates it as (previousNode.End.CFrame - node.Start.CFrame).Magnitude .

nice, ill defenitely use this in my other scripts to avoid dumb if statements

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.