Efficient way of updating player data locally?

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)

Thanks for any help/suggestions!

can I use this to or something for time

A loop simply runs code multiple times, there is no reason for it to “take a massive toll on memory usage”.

I want to avoid using GetPropertyChangedSignal as it doesn’t update as frequently or smoothly as I would like.

It updates instantly as the property updates.

This code calls tick every frame and only runs the actual update every second. Way less efficient.

The best method is to use GetPropertyChangedSignal.

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?

use .Changed then.

Are you updating before the first change? Like

update(level.Value)
level.Changed:Connect(update)

Also, have you tried print()ing the value, and when the event fires?

I was able to fix the issue, instead of putting neededExp into a value, I just made it a varible in the script.

The server-side level up system is equivalent to (xp >= (100 * (level * 1.5)))

So when experience changes the Ui now shows (playerXP / (100*(level*1.5))) And now it updates properly, even upon joining the game.

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