Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

what does 'or 0' mean in datastores and what happens if it is 0?

Asked by 5 years ago
Edited 5 years ago
Yuan.Value = datast1:GetAsync(plr.UserId) or 0 --Right here what does this mean and what happens if it is 0 or not 0
datast1:SetAsync(plr.UserId, Yuan.Value) 

0
or 0 means if the user has no data. This is the data you're giving the player in other words. reparm 91 — 5y
0
so it's saying if the player has no data, set it to this value (0) reparm 91 — 5y
0
so if you wanted people to have 100 Yuan when they are new to the game, set it to `or 100` reparm 91 — 5y
0
This is not how you should be using a data store. User#5423 17 — 5y

1 answer

Log in to vote
3
Answered by 5 years ago

Before I explain what it means in that specific case, you must first know what truthy values and falsey values are. It is a simple matter, so I will not write long of an explanation.

In Lua, truthy values are anything that is not false or nil. Numbers, strings and tables are examples of truthy values.

Falsey values, in Lua, are just false or nil.

  • x and y will evaluate to x if x is falsey, y otherwise.
  • x or y will evaluate to x if x is truthy, y otherwise.
  • not x will evaluate to true if x is falsey, false otherwise.

When there is no saved data in a data store, :GetAsync() returns nil. So that line would be the equivalent of Yuan.Value = nil or 0 if no data was found. This would evaluate to 0 if there was no data, since nil is falsey, and 0 is truthy (fun fact, in most languages 0 is falsey). However with IntValues and NumberValues, assigning the Value property to nil will assign it to 0 so you would not even need the or 0 bit.

Ad

Answer this question