Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How does "and" and "or" work in assigning variables?

I've seen many sample of scripts using "and" or "or" in assigning variables.

Example:

pointsDataStore:UpdateAsync(playerKey, function(oldValue)
			local newValue = oldValue or 0
			newValue = newValue + 50
			return newValue
		end)

I can kinda guess how some of them works, but I still don't get some of them. So I'm asking here if someone can explain them to me.

I also wanna know how this would work:

local character = player.Character or player.CharacterAdded:Wait()

1 comment

1 answer

✓ Accepted answer

First, usage of and can be understood by the link given by @WideSteal321.

Second, usage of or in variables is important to counter nil. Let me explain the scripts that you have provided :

--# Script 1
pointsDataStore:UpdateAsync(playerKey, function(oldValue) --# 'oldValue' is the value that is already saved in the Datastore
	local newValue = oldValue or 0 --# Let's divide the value of variable in two parts : Part A (oldValue) and Part B (0)
	newValue = newValue + 50
	return newValue
end)

The main usage of or comes to assign the variable the value of Part B, if Part A is nil. If someone is saving the value for the first time, oldValue won't exist, i.e., Part A would be nil. And if Part A is nil, it will assign variable newValue the value of Part B, i.e., 0.

Similar to your second script provided :

local character = player.Character or player.CharacterAdded:Wait()

Here, if the character of the player isn't loaded, then, player.Character would be nil and the variable character would be assigned the value of player.CharacterAdded:Wait(), i.e., the player's character.

NOTE You can skip this part if you know what player.CharacterAdded:Wait() does. The main function of :Wait() is pause the script until the function before it is fired. CharacterAdded is fired when a player's character is added in the game world, i.e., when respawned or when a player joins the game for the first time and the character is added to world. :Wait() will make sure that no further script is executed until the player's character is added to the world.

Hope it helps!

8 comments

0

thankyu :3

2

As I quote, from this exact answer: "First of all, usage of and in variables is not a healthy thing.". This is literally not true. To understand why this isn't true you need to actually know what and does, which you can find about here: https://www.lua.org/pil/3.3.html

0

I'm laughing. You've clearly never heard of a ternary statement have you?

0

@Ziffixture My bad, I thought lua had a different structure of ternary statements. Thanks for the laugh tho.

0

Oh jeez I came off as a lot more pissy than intended. Ternary's are inline if-else statements. The 'and' keyword will provide the first result due to the initial input being true, where the 'or' will provide the alternate otherwise.

0

@Ziffixture Yea I know, but as a python main developer, the difference in the languages confuses me sometimes and may result in the error.

0

I completely understand that, I started off with Lua, the number of times I get ArrayOutOfBounds exceptions is unruly :,,)

0

I respect Java

This discussion is preserved as it was. New answers, comments, and votes are closed.