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
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.
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!
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.