I Cloning A Folder In ReplicatedStorage and it goes in The Player and i need to save the values in side the Folder With DataStore Can Anyone Help Me
all Scripts Aree Helpful Thank You.
The Docs are incredibly helpful, I’ll recommend you check them out:
Some good practices to follow when using DataStores
:
- Make sure you name the key you’re saving onto your
DataStore
with the Player’s ID, so you can easily search the key from theDataStore
. - Use pcalls (aka protected calls) when saving or getting data from a
Datastore
! This is because you’re reaching out to Roblox’s servers for information, which means there is a possibility that the request fails and it makes your script error out. I’d recommend you reading this if you need help! - To help the game remember whether it should save data or not (in the event the
Datastore
fails to return data even after trying multiple times withpcall
), create an extra value parented under the Player to indicate whether data should be saved. - As much as possible, try grouping data that are related to each other in a table and saving the table in a single
Datastore
instead of creating multipleDatastores
to save multiple pieces of data. This helps keep things more organised!
If you still aren’t sure how to implement datastores into your use case, I’ll try to give a quick rundown on one way you could:
Player joins the game
Player joins the game, server gets the specific datastore in DataStoreService
used to store the values (see :GetDataStore()
) and checks the Datastore to see if there are any keys with the specified name provided (usually just the Player’s User ID, see :GetAsync()
; remember to wrap :GetAsync()
in a pcall
!).
- If the pcall is successful and the Datastore returns something (other than nil), it means data has already been saved! You can clone your Folder in
ReplicatedStorage
, and use afor
loop to change the Values of each object in your folder to match the data provided in theDataStore
. - If the pcall is successful and the Datastore returns nil, it means it’s the Player’s first time getting their data saved. Simply create a table with a list of deafult values you want the Player to have, and use a
for
loop to change the Values of each object in your folder to match the default table. - If the pcall is unsuccessful even after retrying to get the Player’s data from the
DataStore
(meaning that Roblox servers are most likely down), indicate through the value parented under the Player and kick the Player to save their data.
Player leaves the game
When the Player leaves the game (or when the server shuts down; see :BindToClose()
on how to run code when the game detects that the server is shutting down), the game could use a for
loop to list all the values in the Folder into a table. The game then should save the table using :SetAsync()
wrapped in a pcall
function.
- If the Player has a value indicating that the game should not save the Player’s data, skip this step and
return
instead.
…and that’s about it!
thanks you so much
ill see what i can do.