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)
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)
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)
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
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.)
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.
Edit:
If you turn off deferred events in workspace (Workspace.SignalBehavior to Immediate) this bug stops happening.