Text isn't updating correctly!

I’m trying to have a value smoothly tween on a text label. I’ve done this before and it seems to work in my other games but for some reason not here.

The weird thing is that it does animate up and down almost correctly but is always off by a few digits(e.g. 200 being the correct value but tweening to 206), except whenever I interact with the RobloxStudio gui itself(opening command bar, output, clicking the TextLabel in the PlayerGui itself, etc.) it fixes and automatically readjusts itself to whatever number it should be.

Thanks!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local Formatter = require(ReplicatedStorage.Libs.Formatter)
local CreateTweenInfo = require(script.Parent.Parent.CreateTweenInfo)

local player = game:GetService("Players").LocalPlayer
local shards = player:WaitForChild("leaderstats"):WaitForChild("Shards")
local shardsNumber = Instance.new("NumberValue")
local lastValue = shards.Value

local tween = nil

shardsNumber:GetPropertyChangedSignal("Value"):Connect(function()
	script.Parent.Amount.Text = Formatter:Format(math.round(shardsNumber.Value))
end)
shards:GetPropertyChangedSignal("Value"):Connect(function()
	local difference = shards.Value - lastValue
	local tweenInfo = CreateTweenInfo(difference)

    lastValue = shards.Value
	if tween ~= nil then
		tween:Cancel()
	end
	
	tween = TweenService:Create(shardsNumber, tweenInfo, { Value = shards.Value })
	tween:Play()
end)
3 Likes

Some things to potentially check:

Is the easing type set to elastic?

Is shards.Value a number?

Is it possible this code is causing the problem by setting the text incorrectly?

Have you tried using the recommended .Changed event of value instances instead of getting a property changed signal?

If you added a print statement in here for shardsNumber.Value, and does it print a value larger than the goal value?

Are you missing an initial update to the text box? (I assume you might have that somewhere else.)

This part sounds pretty weird, it doesn’t seem like any part of the code could lead to behavior like this.

Because you store a reference to tween, it won’t be garbage collected and become nil. It might be clearer to do:

if tween then
     tween:Cancel()
end

Though it doesn’t matter in either case because that line only helps with the initialized nil value for tween (in other cases tween always exists, even if its a finished tween).


A quick fix temporary fix might be adding:

local goal = shards.Value
tween.Completed:Once(function()
    shardsNumber.Value = goal
end)

after the line where you call tween:Play().

If that doesn’t fix it there is a bigger problem.

2 Likes

I thought only tables and key value pairs weren’t GC’d when an event is connected, not all values?

Yeah I’ve tried both

I printed the value it returns and the value itself is actually correct, so is the number on each iteration in the shardNumber:GetPropertyChangedSignal(“Value”) connection, but the text itself isn’t set correctly. Actually I take that back, I added a print statement to check what the text is being set to and it does actually print that the text is what it’s supposed to be but on the actual gui itself it’s a few digits behind.

Here’s a vid of the issue appearing on the gui. The numbers in the console are what the text label’s actual text is, but you can see on the gui it’s a different story(sorry for the short vid btw dev forum only allows 10mb or less)

2 Likes

If you store a reference to something in a variable, it won’t be garbage collected. The variable has global scope (not the connection’s function’s scope), so the variable stays even after the connection is ended.

For example:

local part = Instance.new("Part")
part:Destroy()
task.wait(1)
print(part.Name) -- Will still work, the part isn't garbage collected because of the reference

part = nil -- Will now be garbage collected at some point
-- If the part variable was in a function and the function ended, the reference would also end, making the part be able to be gc'ed
1 Like

Could you send the code that’s printing the numbers? Is it directly printing the .Text property?

Could you try replacing the bit above with this:

shardsNumber:GetPropertyChangedSignal("Value"):Connect(function()
	print("Shards Number Changed!")
	print(">\tShard: ", shards.Value)
	print(">\tShard Number: ", shardsNumber.Value)
	script.Parent.Amount.Text = Formatter:Format(math.round(shardsNumber.Value))
	print(">\t.Text: ", script.Parent.Amount.Text)
end)

I think that would help debug it. Maybe copy and paste the last few lines (~20) of the output after it finishes changing.

It’s definitely weird if the .Text property is different from what’s rendered. That shouldn’t happen in any case.

1 Like

It was printing just the straight .Text prop btw.

Here’s what I ended up with, and as you can see on the last line it says all of them are 500(including the .Text prop) but the text stayed at 499 permanently until I changed RobloxStudio’s gui itself as I said in the original post(e.g. clicking the TextLabel in the PlayerGui, opening output, opening command bar, etc., etc.)

Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.076  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.076  >	Shard Number:  362.25560307502747  -  Client - LocalScript:17
  23:17:41.076  >	.Text:  362  -  Client - LocalScript:19
  23:17:41.092  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.092  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.092  >	Shard Number:  372.0097243785858  -  Client - LocalScript:17
  23:17:41.092  >	.Text:  372  -  Client - LocalScript:19
  23:17:41.109  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.109  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.109  >	Shard Number:  382.0072114467621  -  Client - LocalScript:17
  23:17:41.109  >	.Text:  382  -  Client - LocalScript:19
  23:17:41.126  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.126  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.126  >	Shard Number:  391.78818464279175  -  Client - LocalScript:17
  23:17:41.126  >	.Text:  392  -  Client - LocalScript:19
  23:17:41.144  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.144  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.144  >	Shard Number:  402.7255177497864  -  Client - LocalScript:17
  23:17:41.145  >	.Text:  403  -  Client - LocalScript:19
  23:17:41.159  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.159  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.159  >	Shard Number:  411.1205041408539  -  Client - LocalScript:17
  23:17:41.159  >	.Text:  411  -  Client - LocalScript:19
  23:17:41.175  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.175  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.175  >	Shard Number:  420.71443796157837  -  Client - LocalScript:17
  23:17:41.175  >	.Text:  421  -  Client - LocalScript:19
  23:17:41.192  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.192  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.192  >	Shard Number:  430.7842552661896  -  Client - LocalScript:17
  23:17:41.192  >	.Text:  431  -  Client - LocalScript:19
  23:17:41.210  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.210  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.210  >	Shard Number:  441.17072224617004  -  Client - LocalScript:17
  23:17:41.210  >	.Text:  441  -  Client - LocalScript:19
  23:17:41.226  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.226  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.226  >	Shard Number:  450.9201645851135  -  Client - LocalScript:17
  23:17:41.226  >	.Text:  451  -  Client - LocalScript:19
  23:17:41.243  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.243  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.243  >	Shard Number:  460.8181416988373  -  Client - LocalScript:17
  23:17:41.243  >	.Text:  461  -  Client - LocalScript:19
  23:17:41.259  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.259  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.260  >	Shard Number:  470.44166922569275  -  Client - LocalScript:17
  23:17:41.260  >	.Text:  470  -  Client - LocalScript:19
  23:17:41.276  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.276  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.276  >	Shard Number:  480.33860325813293  -  Client - LocalScript:17
  23:17:41.277  >	.Text:  480  -  Client - LocalScript:19
  23:17:41.293  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.293  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.293  >	Shard Number:  490.0660216808319  -  Client - LocalScript:17
  23:17:41.293  >	.Text:  490  -  Client - LocalScript:19
  23:17:41.308  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.309  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.309  >	Shard Number:  499.29746985435486  -  Client - LocalScript:17
  23:17:41.309  >	.Text:  499  -  Client - LocalScript:19
  23:17:41.326  Shards Number Changed!  -  Client - LocalScript:15
  23:17:41.326  >	Shard:  500  -  Client - LocalScript:16
  23:17:41.326  >	Shard Number:  500  -  Client - LocalScript:17
  23:17:41.326  >	.Text:  500  -  Client - LocalScript:19
1 Like

Yeah that’s definitely a bug then.

I did some searching and found an unresolved bug report for this:

This description is pretty similar to what you’re describing:

Maybe bump that thread with your case. Perhaps give a simplified repro file if you get the chance.

Out of curiosity, if you remove the UI effects (UI outlines, UI gradients, CanvasGroups, etc), does it start to work? The bug looks like something to do with re-rendering certain UI elements, and I know some of those UI effects and definitely CanvasGroups change the re-rendering pipeline. If you find it does that would definitely be helpful information for the bug report.

I posted something there, it’s a bug from the new deferred events.

Sorry this doesn’t seem to be something that can be fixed!


Edit:
Per the bug report, if you add a task.wait() (technically the same as waiting for heartbeat) to the shardsNumber.Changed connection’s function, it should work (you shouldn’t need to do that though).

I guess if it gets an update around the renderstepped frame area it just totally ignores that it needs to re-render the UI.

image

Edit:
If you turn off deferred events in workspace (Workspace.SignalBehavior to Immediate) this bug stops happening.

1 Like

Thank you so much! Turning off deferred events actually solved the issue!

Hopefully they’ll fix this soon seeing as the bug was reported over 2 months ago, but for now disabling deferred events worked! Again, thank you :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.