Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Trying to make a DataStore script, where can I learn how to do it correctly?

Asked by 6 years ago

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?

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

0
Thanks for the advice, I just have one main question: First of all I tried using the script and it gave me an error code saying "You must publish this place to the web to access DataStore", this isn't really a problem as I expected this, but the main thing I wanted to know is: how would I change a DataStore value in an external script? EnderGamer358 79 — 6y
0
external script? You mean like a local script? User#20388 0 — 6y
0
or a module script? User#20388 0 — 6y
0
If you mean from a local script you should use a remote event with the value you want to save inside and then setasync that value User#20388 0 — 6y
View all comments (10 more)
0
To get the value from a local script you would have to use a remote function User#20388 0 — 6y
0
Also, if you want to use datastore in roblox studio go to your game settings and allow game acces to api services (and please mark this as best answer :P) User#20388 0 — 6y
0
I mean like any script that would require the elements from the data store, for instance if I wanted to make a money value and store it in DataStore, how would I "grab" it to use for purchasing items? EnderGamer358 79 — 6y
0
Also I don't have much skill with remote events and such, could you tell me what code I'd have to use or where I could learn about them? EnderGamer358 79 — 6y
0
Datastore works with all scripts, you can just use 'money.Value == PlayerSave:GetAsync(key)' and it would work as long it is a serverscript, you can learn about remote events here: http://wiki.roblox.com/index.php?title=Remote_Functions_%26_Events It tells you about everything about them User#20388 0 — 6y
0
Thats helpful and all but a little backwards, I don't want to change an in game value due to the DataStore, I want to change a value in the DataStore due to an in game value. How would I do this? EnderGamer358 79 — 6y
0
On the other hand I'd actually like to do both, but could you give a detailed explanation on how I would do both changing an in game value from the DataStore and a DataStore value from an in game value? EnderGamer358 79 — 6y
0
to change an ingame value use plr.Stats.Money = PlrStore:GetAsync(plr.UserId) (the datastore needs to be called PlrStore and plr.Stats.Money needs to exist ofcourse lol. To change the datastore use setasync PlrStore:SetAsync(plr.UserId,money) money is the value you want to change it to this can be a variable or a number or a string or a bool or a table User#20388 0 — 6y
0
Thanks a lot man, it's finally working :) EnderGamer358 79 — 6y
0
Np, Glad I could help :P User#20388 0 — 6y
Ad
Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
6 years ago

Watch these two videos I made on DataStores.

https://www.youtube.com/watch?v=2NPqKR9D2gQ

https://www.youtube.com/watch?v=zY53itnBchE

Answer this question