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