I've been working on a roblox game and I wanted to make a save script for it. I searched up some scripts in the toolbox but it turns out they are a WHOLE lot harder to understand than I thought. I also tried doing the do-it yourself approach by examining the script and figuring out how it works, but I couldn't make heads or tails of the hundreds of lines of code in the scripts. The roblox wiki was absolutely no help either. Is there a good place I can learn how to make a script (that uses Datastores) for saving models, intvalues, and leaderstats?
Datastore looks difficult, and can be very difficult when you don't understand it, and I agree with you completely that wiki isn't the best place to learn
That's why I'll try to explain, first, you CANNOT save instances, however, you can save all the data required to build them, like position. You can save bools, ints, tables,...
First thing you always have to do is get the datastore service
local DataStore = game:GetService("DataStoreService")
You also have to get the datastore you want to use
local PlayerSave = DataStore:GetDataStore("PS_"..dataNum)
You could just use 'PlayerDatastore' or anything really but if I want to reset a datastore I just have to change a variable and done, you CANNOT delete a datastore!
To set a value to the datastore you have to use SetAsync or IncrementAsync, I never use the second one though
You also have to use a key, the key is mostly the players userId, so the datastore isn't for one player only
Example
local DataStore = game:GetService("DataStoreService") local PlayerSave = DataStore:GetDataStore('PlayerDataStore') game.Players.PlayerAdded:connect(function(plr) local key = plr.UserId local money = plr:WaitForChild('Money') PlayerSave:SetAsync(key, money.Value) end)
Ofcourse you will never use the example above, because this sets a value on a players spawn.
You can get a value from a datastore you have to use GetAsync, like this, you don't have to type in your variable next to the key, just put it in a variable like in the example
local DataStore = game:GetService("DataStoreService") local PlayerSave = DataStore:GetDataStore('PlayerDataStore') game.Players.PlayerAdded:connect(function(plr) local key = plr.UserId local money = plr:WaitForChild('Money') or 0 money.Value == PlayerSave:GetAsync(key) end)
Important: in local money = plr:WaitForChild('Money') or 0 I add the *or 0 *because if the player has never played before we need to set his stats to 0
Also important: Datastores have limits, if you keep sending data to the datastore it will maybe lose that data, so it's important you don't let the players save all the time, you could use this in a loop to 'autosave'.
If there is anything more you don't understand, or ask it, or try to find it here http://wiki.roblox.com/index.php/Data_store I wouldn't recommend watching videos as they use bad scripts most of the time
Also, error handling is very important, not neccesary but if you have a huge game you should look into it, good luck :P
Watch these two videos I made on DataStores.
https://www.youtube.com/watch?v=2NPqKR9D2gQ
https://www.youtube.com/watch?v=zY53itnBchE