Optimizing Datastores System

So, recently, i’ve switched my datastores system from using instance values to a table based system, where data is fully handled in server using a table (Default data table below).

image

The system works nicely and is much more efficient, but i’ve come with an issue: This system causes extreme lag spikes client side when handling data in any way. The way im doing this is to fire a remote event when data is done being changed and then have a client script update every UI necessary, but this makes client side freeze for a concerning amount of seconds before working again. Here’s the client code:

image

image

I’m aware the issue might be on the fact i’m requiring 6 modules each time, but there’s no other way i thought i could do the job, as i can’t use .Changed event for each script because data uses a table. Anyone knows a better way of doing this? Thanks for reading!

I assume each of the modules you require runs specific UI update code?
Why not require each module once at the start and return the UI update function to be kept on the client. You can then perform checks on any changes to the data and run the specific functions that need to be run. e.g.

local clientData
local updaters = {
    ["Gold"] = require(goldModule),
     --other modules using data keys as keys...etc
     --each module would need to have an '.update(Data)' function within it.
}
local function checkData(Data)
    for k, v in Data do
        if v ~= clientData[k] then
            print("updating:", k)
            updaters[k].update(Data)
        else
            print("no update required for", k)
        end
    end
end
func.OnClientEvent:Connect(function(Data)
    if not clientData then  --on first update to set initial clientData
        clientData = Data
        for i, v in pairs(updaters) do
            v.update(Data)
            print("updating all")
        end
    else
         checkData(Data)
    end
end)

Something like that (I’ve not tested this, just an idea of how it would work, EDIT: in fact this definitely won’t work as not every Data key has an update module, but this could be fixed with a few extra logic steps when comparing the Data to the clientData)

Finally got rid of lag after a few adjustments and adding support to compare tables. Thanks a lot!

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