How do I make a save Leaderstats gui?

Quick question:

So, I have been making a game, and I took a model from a YouTube tutorial and I realized that the leaderboard script didn’t have a Data Store, therefore it wouldn’t save. Does anyone know how to make a save button gui that when the player clicks it, it saves all their stats and it will automatically load (or there can be a load button that loads their stats manually) so the player won’t have to restart everything?

Why don’t you just make it auto save? This just seems like a waste of time.

1 Like

Creating a button to save a user’s data is quite simple :slight_smile: There are numerous ways to approach this.

Let’s assume that you already set up your Datastore. With that, we can start working towards our goal.

Firstly, we need to create a button, you may do so to your liking. Now we need to program your button so that it can signal to the server that the player wants to save.

It could look something like this:

     RemoteEvent:FireServer() --It's wise to pass the least amount of data from the client to server. In this case, we don't really need to pass any data to the server.
--It might also be in your best interest to keep a cooldown to prevent your datastore from throttling which could result in data loss :)

To communicate with the server, we need to see when the Remote Event was called:

In the server script containing the datastore, you can simply write the following:

     local Success, err = pcall(function()
           Datastore:SetAsync(key, value) --You will have to configure this yourself. Set up a key for every value. A better way would be to save tables rather than individual values!!
      if Success then print("Saved yay") end

I have yet to call this the most efficient system for saving data with the press of a button, but this is a basic overview of what you should base your general system around

1 Like

i literally said “from a youtube tutorial” i have no scripting experience :smile:

Then I’d recommend trying to learn, and stop ripping off others people’s tutorials. If you want to learn, I would truly recommend this book. Even if you don’t like reading, or just don’t think it’ll be helpful, trust me, it will. Please, please, please. For real, check this book out.

1 Like

What I’d do is this:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Your Datastore Name")
     local success, err = pcall(function)
          DataStore:SetAsync(player.UserId, game.Players[player.Name].leaderstats.(Your Lederstats Name).Value
     if success then
          warn("There was an error, please try again.")

Has worked for me.

Feel free to ask me any other questions or if this didn’t work, make sure you’ve got this option enabled:

(If you’re testing this in Studio. Or just try it in a live server.)

1 Like