How to Make a Client Be Prepared Before Doing Anything Else!

Introduction

Recently, I came across a problem where a client had to use a remote function to get datastore data from the server, but the server wasn’t done preparing the client’s datastore yet! This caused an error, so I had to create a workaround for this. I was thinking maybe this would be helpful for others, so I decided to make this tutorial.

Steps

Step 1: Create a module script and name it ReadyClientEnum. Add this script into ReplicatedStorage. In this script, delete everything and add this:

return {
  ExampleKey = false,
}

Step 2: Make as many keys as you want. Name them to the things you want to prepare. Make sure you set them all as false, and don’t make more than one of the same key!
Example:

return {
  Datastore = false,
  Leaderstats = false,
  GUI = false,
}

Step 3: Now create a remote event and name it ClientIsReady. Place it into ReplicatedStorage.

Step 4: In any client script, put this inside:

local ReadyClientEnum = game.ReplicatedStorage.ReadyClientEnum
local clientIsReadyRemote = game.ReplicatedStorage.ClientIsReady

local function isReady(): Boolean
   local result = true
   for key, keyIsReady in ReadyClientEnum do
      if keyIsReady then
         result = false
         break
      end
   end
   return result
end

clientIsReadyRemote.OnClientEvent:Connect(function(nowReadyWith: string)
   if ReadyClientEnum[nowReadyWith] == nil then
      error(nowReadyWith .. "is not a key in ReadyClientEnum")
   end

   ReadyClientEnum[nowReadyWith] = true
end)

repeat 
   task.wait()
until
   isReady()

--Now do whatever you want to here for after everything is prepared!

Step 5: Now for your server side. Let’s say you want to prepare a client’s data store before the client does anything, this is what you would do in a server script:

local clientIsReadyRemote = game.ReplicatedStorage.ClientIsReady

local function onPlayerAdded(player)
   -- Blah blah blah add data store
   clientIsReadyRemote:FireClient(player, "____")
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

Now, this is the tricky part. The reason why I used ____ is because you need to make sure it’s the same exact name as your key in the ReadyClientEnum. For example, in the ReadyClientEnum, it would look something like this:

return {
   Datastore = false,
}

So in the ____, MAKE SURE it’s the exact same name as your key. So the final result of the FireClient line would look like this:

   clientIsReadyRemote:FireClient(player, "Datastore")

Reminders and More

• Again, you can do this as many times as you want. You can do a Datastore key, Leaderstats key, avatar loaded key, anything like that.
• You can use BindableEvents if you want to use client related stuff, like preparing a gui to make it fit the screen properly, or using PreloadAsync to make sure everything in the map is loaded.

Hope this helped!

If anyone has any questions, please reply in the replies. See you later!

5 Likes

Short & simple while pretty useful. Good work.

1 Like

I feel like you’re overcomplicating things, you could just set a boolean attribute called “DataLoaded” to the player in server side when they’re joining, load the player datas and set it to true when all datas are loaded, then in client side, wait for it to be true to setup gui’s.


Here’s how mine look like
Capture

7 Likes