This probably seems like a stupid question, but is there any difference between these 2 lines of code?
I created a quick and simple script for a time played leaderboard that goes up by 1 for every second the player is in the game.
game.Players.PlayerAdded:Connect(function(player)
local playerStats = Instance.new("Folder")
playerStats.Parent = player
playerStats.Name = "leaderstats"
local playtime = Instance.new("IntValue")
playtime.Parent = playerStats
playtime.Value = 0
local function addTime()
while true do
--playtime.Value = playtime.Value + 1
playtime.Value += 1
task.wait(1)
end
end
addTime()
end)
Inside of the while loop, there are 2 lines of code.
I was curious, is there any difference between these 2? are there situations where you would use 1 over the other? I know they work exactly the same, but i was wondering if one is better to use than the other. Thank you!
Compound assignment also has a slight performance boost because it only evaluates the assignee once. So in addition to being more concise and easier to type, it just runs better. There’s genuinely no reason why someone would purposely not use it
In terms of Roblox, this also means only evaluating the instance hierarchy once, doing something like NumberValue.Value += 1 will only evaluate the pathing for .Value once which can be a big deal if you’re writing to it thousands of times a second