Which can/cannot you cache?

So, I was writing this block of codes… I wanted to follow the DRY Principle :stuck_out_tongue:

coroutine.wrap(function()
	blockAnim:Play()

	local walkSpeed = humanoid.WalkSpeed
	local jumpPower = humanoid.JumpPower

	walkSpeed = 5
	jumpPower = 5

	wait(1)

	blockAnim:Stop()

	walkSpeed = 16
	jumpPower = 50
end)()

which did not really turn out fine so I tried this instead; which actually worked:

coroutine.wrap(function()
	blockAnim:Play()

	humanoid.WalkSpeed = 5
	humanoid.JumpPower = 5

	wait(1)

	blockAnim:Stop()

	humanoid.WalkSpeed = 16
	humanoid.JumpPower = 50
end)()

So, my question is, what can you cache (put in a variable and use) and what can you not? Answers/tips are greatly appreciated! <3

local walkSpeed = humanoid.WalkSpeed

Here you are setting a variable equal to the value of an object’s property, not an evergreen reference to that property. In other words, it is the value of the property in that exact moment. Printing your variable later will return the same number, no matter how humanoid.WalkSpeed has changed.

Your second bit of code works because you read the value of the property each and every time you need it.

2 Likes