How to save and load values in a gui by using DataStore

Title pretty much explains it all. How would i save and load multiple values within a player gui, via DataStore. I do not know much about the DataStore, but i do know its used for saving and loading stuff

Heres a screenshot of what im trying to save and load: (if the “Owned” values within the folders are equal to true)
image

You can’t.

You’d need to use Remote Events to do this, and load the DataStore on Server, since DataStores can be accepted only on Server!

i didn’t say that this was a client only thing. If anything this is a server thing. this is because i only change these values with only server side scripts.

I’m saying that you should store this stuff in for example ServerStorage.

You’ll access it from server anyways.

You should probably put the values inside the players so you only need to load them once, then change the data in the values accordingly using DataStores, when player leaves or game shutdowns you just save the values val…

To explain the concept :

  1. Player joins you get the players stats ( from the datastore ) and create the player its own values
  2. Change the values to the players stats and place them inside the player ( whatever.Parent = Player )
  3. Whenever you’re updating data and the player is in game just change the values value
  4. When the player leaves save the values value inside the datastore ( and maybe having a timer every x amount of time is good )

I suggest putting all of those stuff inside 1 folder called “Data” and then placing it inside the player.

Also I just reread what you said, I am sorry that I kinda answered a completely different question.

To use DataStores and load different values and load them I suggest saving a table :

local PlayerData = {
    Weapons = {
        ["Classic Sword"] = {
            Equipped = something,
            Owned = something
        },
        ["Wooden Sword"] = {
            Equipped = something,
            Owned = something
        }
    }
}

And then when the player joins you just do the concept I said above and in the local script you check those values.

4 Likes

You’re not supposed to use server scripts for PlayerGui instances when the game is running.

Anyway, yes, you’ll need RemoteEvents because you’ll need to communication between server and local scripts. The way it works is simple.

  1. Local script requests DataStore info to be displayed from the script
  2. Server script retrieves data, and sends that player-specific data to the client
  3. Client receives data and your GUI stats can be updated
1 Like

I do not recommend this because people can easily hack the values and theoretically become too overpowered. Because you cannot control what a player does to it’s own client

Yes, putting the values in the players is important but there’s an issue for some game designs. If you want to check for buying or otherwise, you can’t use the client’s stats so you have to request from the datastore you just updated. This can lead to datastore queues if you don’t handle it properly. Suffice to say, it is much safer to just store the stats on ServerStorage - it causes no harm whatsoever, is much more organized, and is faster in the retrieval process as well.

But like you said, using a table is a good way to save/load data.

For OP, if you want to assign the data from the table to your server stats, look for the table’s keyname (ex. “Weapons”) if you have a weapons folder, then create a value (idk if you use number IDs or strings) in your Weapons Folder in the server for the player you are loading the data, for all the weapons they have in the table.

You do this for all the stats/inventory. So obviously, you need to organize your table the same way you organize your ServerStorage inventories. Ideally,

UserId :arrow_forward: Item/Class/Stat Name :arrow_forward: Items/Stats the player owns.

Hope that helps

If you read my post you would see that I have said how you’d update the values and it doesn’t contain getting data from the datastore,
Saving the values in ServerStorage and inside the players would be the same thing except when the player leaves you don’t have to delete the values from the player while if you put it on ServerStorage you need to delete it yourself.

I said to put it in the player because OP wants to use them for PlayerGuis I think that the values would be perfect if he were to check whenever the value is changed and change the values from the server because they’ll obviously replicate, putting them inside ServerStorage will make it so OP has to ask the server for the stats everytime he needs to update something or check if something changed… ( well he could do RemoteEvents and check for OnClientEvent but yeah… )

Edit: Did not know player info is not replicated to server

However I’d like to add that if you need hidden stats, you might as well save your hidden stats on the server so the client can’t access them :wink:

1 Like

Ah I guess you misunderstood me,I am bad at explaining sometimes here let me reword my posts :

When the client changes something that’s under the Player Object it Will not replicate to the server,
so there is no reason for you to be scared if it has been tampered with by the client ( After all you’re going to handle the purchases on the server ).

Whenever you update someones stats you simply change the value of said stat inside the Player Object and it’ll replicate both to the server and client so getting changes is as simple as checking when the value is changed ( Inside the client you do Value:GetPropertyChangedSignal(“Value”) )

Saving the data can’t be any easier!
Either you have a loop every x amount of time and update the data in the datastore or you could just update it whenever the player leaves. ( game:BindToClose and Players.PlayerRemoving )

2 Likes

Oh i see, but how would i make this compatible with the system im currently using for my gui?

I do, i store my tools in server storage, but i have the information about the tool in a gui. I would just like these values to save and load. Its honestly the exact same as having a leaderstats inside the player. Instead its a gui containing the information. I just do not know how to save these values and load them

I believe BasedFX answered your question very well, I recommend you to check out how arrays work.
You need to use the same concept as what he provided:

    Weapons = {
        ["Classic Sword"] = {
            Equipped = something,
            Owned = something
        },
        ["Wooden Sword"] = {
            Equipped = something,
            Owned = something
        }
    }
}

The difference here is that instead of just having the two weapon names, you’ll need to make more items in the Weapons array for your other weapons (if you have any).

Note: Make sure you cache/store the data in the .PlayerRemoved event when the player leaves as soon as possible.

Since you’re quite new to DataStores, you should read about how they work: Data Stores | Documentation - Roblox Creator Hub
Here is also a video if you’re more of a visual/practical learner: https://www.youtube.com/watch?v=DkYupSBUpes

Just a question. Is this not the same as having a folder containing these same values? why do i have to use a script or a Module all of a sudden? couldn’t i do something like

local PlayerData = {
    Weapons = {
        ["Classic Sword"] = {
            Equipped = Folder.Item.Equipped.Value,
            Owned = Folder.Item.Owned.Value
        },
        ["Wooden Sword"] = {
            Equipped = Folder.Item.Equipped.Value,
            Owned = Folder.Item.Owned.Value
        }
    }
}

or something like that?

In some sense it is the same but it just makes it tedious to code. It’s good practice to store data in a table rather than using folders and values. In some cases it may be better to do otherwise but you should only really use arrays to handle and store data.

It is also good practice to store data in modules for giving a modular approach to programming. Modules can be called from different scripts depending on where it’s located. It allows you to keep your programming centralised stopping you from being more liable to make logical errors.
If you’re not much familiar with modules, please check it here

How would i accomplish this scripting wise? i am not the most experienced scripter and i could use some help.

I’m using a server script to check if these values are true.

You can’t Set/Get Async with DataStore on local scripts, u need to use Server Scripts

i know, this is why im using server scripts