Recently I started studying the different types of operators in scripting. I am currently learning about compound assignment. But then I found this sentence:
The operation in a compound assignment occurs once. For example, if an expression generates a random index in a table, Luau uses the same index for both the operation and the assignment."
I can understand the first sentence but the second is so confusing. Is the expression a compound assignment? Can someone further explain this sentence to me, thanks.
It means the operation in a compound assignment occurs once
Example:
local t = {5,10,15,20}
t[math.random(#t)] += 1
-- Assume math.random returns 3
--> t = {5,10,16,20}
Here, math.random(#t) is only evaluated once even though if you were to write it out fully it would be:
local t = {5,10,15,20}
t[math.random(#t)] = t[math.random(#t)] + 1
-- Assume math.random returns 3
-- Assume the second math.random returns 2
--> t = {5,10,11,20}
Here math.random(#t) would be evaluated twice giving you unintended results