Why does the script have this value

I’ve been looking at some scripts and noticed that one of them had a line like this:

local Value = workspace.Part and workspace.Part.Position

And im wondering what does it do since when i print the value it still shows the position and not the part

Honestly that makes no sense, it would make sense if it was written like this,

local part, position = workspace.Part, workspace.Part.Position

There’s some cases that you would use something similar to that but instead of “and” you’d use “or”. Here’s an example for that as well,

local value = someValue or 10

In case “someValue” is nil, it would use 10 instead.

I not sure maybe to check if the part exists
local Value = workspace:FindFirstChild("Part") and workspace.Part.Position
if part does not exist Value will be nil but if part does exist Value will be the part position

1 Like

I think this might be it actually

That is the reason, but the code in the original post is bad.

local Value = workspace.Part and workspace.Part.Position

I would like to emphasize that this code will error if Part doesn’t exist. As you have shown, FindFirstChild is the correct method to use because it will not error.

@datboyPiter this works because in Lua, the most recently evaluated expression is returned by and or or. You’ve seen people do this I assume?
local char = plr.Character or plr.CharacterAdded:Wait()
In this scenario, if plr.Character is not nil, then it doesn’t need to run plr.CharacterAdded:Wait() because or only needs one side to be non-nil/non-false. If I were to switch it for and, then if plr.Character was nil the second side would never run. This is because with and, both sides need to be non-nil/non-false and if one side is nil or false, it can just give up because the other side doesn’t matter.

You can of course pair them as well:

local a = 1
local b = 2
local c = false
local d = (c and a) or b
-- if c is not nil and not false, evaluate a.
-- if a is not nil and not false, return a because the only other expression is `or`.
-- if either c or a are nil or false, return b.
-- if c is false, this returns 2. if c is true, this returns 1.

In all scenarios and uses I have covered however, it is preferred to use the new Roblox-specific Luau ternary expression syntax:

local Value = workspace.Part.Position if workspace:FindFirstChild("Part") else nil
local a = 1
local b = 2
local c = false
local d = a if c else b -- an analogue to my earlier example code

Okay i pretty sure i understand now. Using and in a value sets it only when both of the parameters are non-nil / non-false correct?

Yeah pretty much. But it will always return the most recently evaluated element anyways, no matter what that is.

local a = nil and 5 -- won't return 5 because there's no point
local b = false and 10 -- won't return 10 because there's no point
print(a) --> nil
print(b) --> false

local c = nil or false -- will return the second element (false) because the first element is false or nil
local d = false or nil -- will proceed to return nil because the first element is false or nil
print(c) --> false
print(d) --> nil

local e = 5 or false -- will *not* proceed to the second element because there's no point
local d = 10 and false -- *will* proceed to the second element because both sides are required to satisfy 'and'
print(e) --> 5
print(d) --> false

They made it this way for convenience, both for them and for us. Values are considered ‘truthy’ if there are not nil and not false. So 5 is truthy, “test” is truthy, and true is truthy.

if 5 then print(5) end --> 5
if "test" then print("test") end --> test
if true then print(true) end --> true

As such, there is no point for and and or to return true or false, they can just return whatever element was last evaluated. I learned two days ago that this is also how JavaScript works.