Data doesn't load/load fast enough

So, I have an localscript which works mainly on data from the server, and the localscript recieves the data from a remotefunction.

Sometimes it works and it loads, but sometimes it doesnt and the whole script starts outputting errors because it did not get recieved/took too long to recieve.

How would i lower the chance of this happening/completely stop it?

line getting the data:

--at about the start of the script
local data = game.ReplicatedStorage.Game.Events.GetPlayersData:InvokeServer()
repeat wait() until data ~= nil or data ~= {}

serverscript:

game.ReplicatedStorage.Game.Events.GetPlayersData.OnServerInvoke = function()
	return data
end

We need a code example so we can see, where could be the problem.

You you send the scripts managing and sending the data over to the client side? I accidentally replied to the wrong person lols

i edited it to include some of the code

That’s not how it works. Thread is hanged until server responds with the data, so the line with the repeat wait is nonsense.
If you’re getting no data, then the server cannot obtain the data. The problem is on your server side.

i found a fix to my problem, i had to keep on requesting that value till it returned with a not empty table,
because localscripts run earlier than serverscripts.

Server scripts always run before local scripts, since server is always started first. That’s definitely not the issue, issue must be somewhere else, and spamming your server with requests until it gives you something is definitely not good solution to the problem.

How would i go around fixing that then?

As I said before, there must be a problem on your server side, where it doesn’t just provide you the data for some reason, you should debug that.

it just returns the data, but only after a few seconds because the datastore has to load in first

Yes, naturally. That’s why the client’s thread is hanged until server loads the data and then returns it to the client. That’s why you’re using RemoteFunction at the first place. Nothing is instant.
Also read something about DataStore’s limit, because there’s a chance you’ve exhausted your datastore budget and so datastore will queue your request until you get a budget. Datastore requests should never be spammed!

This should contain all important information on datastores: Details on DataStoreService for Advanced Developers

2 Likes

Sorry for not answering, i already found the solution but just forgot about this post.

I had to change the data in the local script only whenever the data itself updates too.
i personally had to fire an event to the client returning the new data and changing it whenever the event of updating the data in the server script is fired aswell.

it saves space and is 10x less laggy aswell.

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