People using "or" in variable defining

I always see professional scripters using “or” while defining variables,
Why do they do this?

function CommandSystems.SystemMessage(Message, Prefix, RGBColor)
	local TextChatService = game:GetService("TextChatService")
	local RemoteEvent = game.ReplicatedStorage.SMessage
	 Prefix = Prefix or "[Server] "
	 RGBColor = RGBColor or Color3.fromRGB(255,255,255)


	task.wait(1)
	RemoteEvent:FireAllClient(Message, Prefix, RGBColor)
end

heres some example code, please explain why.
All help is appreciated

2 Likes

It serves as a fallback if the first variable isn’t there. Ex: if rgbcolor doesn’t exist it will use the color3.fromrgb value. It is good practice to do this if you are unsure if a variable is there and don’t want to use waitforchild.

2 Likes

From what I understand, the second value, the one after the or is used if the first one is nil/false
for example

local a = nil
local b = 'hi'
print(a or b) --hi
a = 'hello'
print(a or b) --hello
2 Likes

thank you both of you, I understood how it is used.

1 Like

As for “why,” It looks better than doing

if not RGBColor then
    RGBColor = Color3.fromRGB(255, 255, 255)
end

All it is is cleaner code, really.

2 Likes

just to add on, there’s another thing that’s similar:

local isBlueApple = true
print(isBlueApple and 'its a blue apple' or 'not blue apple') -- its a blue apple
isBlueApple = false
print(isBlueApple and 'its a blue apple' or 'not blue apple') -- not blue apple

i don’t know why i chose blue apples for my explanation lol.

it’s just a shorter way of saying

if isBlueApple then
   ---blah blah
else 
    --not blue apple
end

but personally i like the second one better because its more readable imo.

1 Like

lol
thanks for making the explanation so clear, i couldn’t find anything like this on any other website

1 Like

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