My game utilizes DataStore2 to create and save player statistics; However, I also copied the server-side statistics into ReplicatedStorage for easy access by the client, that way I don’t need to incessantly use RemoteFunctions to call for the data from the server when updating the player’s UI.
In the past I have used
while true() do
wait()
-- Update player gui to reflect player stats here
end
Although these loops can add up throughout a game’s code and take a massive toll on memory usage, and they are relatively unoptimized.
Any idea what other method I could use or how I could optimize while true do loops? Would Heartbeat be a good method?
I want to avoid using GetPropertyChangedSignal as it doesn’t update as frequently or smoothly as I would like.
Possible solution:
local runService = game:GetService("RunService")
local interval = tick()
runService.Heartbeat:Connect(function()
if tick()-interval < 1 then return end
interval = tick()
-- Update player gui to reflect player stats here
end)
GetPropertyChangedSignal works but not as I want it to. When I join the game most of my data loads as it should; However, my player experience keeps having issues.
Coin amount updates, experience amount updates, and the gem amount updates; however, the experience needed to level up doesn’t update and keeps loading as zero visually even though the value in replicated storage is updating upon loading data.
i.e. PlayerExp = 60 and NeededExpToLevelUp = 320
The game loads and the UI shows (60/0) instead of (60/320). It only updates to the proper neededExp after more experience is gained (i.e. collecting 20 exp makes the UI now show as (80/320) instead of (60/0).
I even tried debugging and did
if neededExpToLevelUp.Value == 0 then
repeat task.wait() until neededExpToLevelUp.Value > 0
-- update Ui to display new XP/NeededXP
elseif neededExpToLevelUp.Value > 0 then
-- updateUi to display new XP/NeededXP
end
But then nothing ever loaded or updated (not even the coins and such that came after the if statement) even though the value was indeed updated to the proper value above 0.
Any ideas on how to solve an issue like this?