How do i use this data save feature?

How can i save this set of data into a data store where it saves the players data when they leave and loads in when they join back?


i tried researching various guides on how to save it but the results dont give me what im looking for (i want the currentcart value to be saved outside the leaderstats folder)

1 Like

Check out this tutorial, helped me a lot.

-- Put this code below everything else in the playeradded event
local success, data = pcall(function()
return datastore:GetAsync(player.UserId)
end)

if data and success then
for key, value in data do
if key == "Coins" then
coins.Value = value
elseif key == "CurrentCart" then
CurrentCart.Value = value
end
end
game.Players.PlayerRemoving:Connect(function(player)

local data = {}

for _, stat in player.leaderstats:GetChildren() do
data[stat.Name] = stat.Value
end

local success, error = pcall(function()
datastore:SetAsync(player.UserId, data)
end)

end)

I’ve made my own tutorial on how to make a basic datastore, I suggest you check it out:

From what i’m seeing in the thumbnail it seems the tutorial has forgotten to wrap these async functios in pcalls since they send web requests to roblox they tend to fail at times.

oh yeah mb, wrong video, i had 2 saved.
Theres this one that can help a bit, he even explained (shortly) why use pcall.

Honestly, I would just recommend using the documentation’s tutorial. It’s what I used, originally when I was newer to scripting I did look up a tutorial but after some time I became better at using the docs and haven’t used tutorials from youtube in ages.

Especially since youtube tutorials usually spoonfeed instead of actually teaching, with the documentation’s datastore tutoiral you can throughly understand why things work the way they do.

I mean, yeah docs are way better than tutorials, they cover pretty much everything about the topic.
Tutorials are my second source, I’ve been scripting for less than a month, so I don’t get there right away in many things, and some tutorials are usefull sometimes.

Ah alright, I recommend sticking devking’s beginner series then moving on to devking’s advanced series. After that, you should be able to reference from the docs quite easily. 9 months into scripting now I realised how bad youtube tutorials are, devking is mid but he’s the best one out there that atleast teaches things that noobs can understand.

But devking also makes tutorials on how to make whole systems aswell, so skip over those.

You might also wanna learn roblox physics so I recommend watching B ricey for that.

I haven’t used a tutorial on youtube for ages though, and when I used to I watched tutorials that actually teach how to use things instead of telling you how to make an entire game or system. I think that’s why I was able to learn so quickly and reference from the documentation solely after a few months.

The systems they give you are also pretty trashy rather than making them yourself, usually they give unoptimized bad code and if you made a game solely of a tutorial it would probably never get popular.

From just one month into scripting, I was able to make a semi-unique game that got almost 200 visits now. It’s not the best game but I was proud of it and for one month experience it’s quite good.

Just make sure to only use tutorials that teach you how to use something instead of copy-paste.

yeah I finished both devking courses then started doing random ideas and now I’m helping on a small project for an rpg game, wich actually helps to learn things that I probably wouldn’t have learned very soon if I continued on my own. Alr thanks for the advice.

Good work, you’re on the right track.

I recommend you to use storage system (store all data in a module or one module per player, as you decide) and then save in a datastore frequently and when player’s leave/game shutdown.
And also add a Connected state to your data to prevent player from saving two different data version because this same player is somehow connected on the same game on a millisecond time manner.