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

How do I use data store to set a BoolValue to true if a player has been in the game before?

Asked by 10 years ago

I know this is not a request site, but I'm not asking for a free script, I'm asking for a teacher. The wiki is very, very confusing, and I want to know how to use data store. I would greatly appreciate an answer that explains how to accomplish what was explained in the title (assume the BoolValue is in the player). Please use language that I can understand, not crazy, confusing, big words from computer science.

I know some people might say something like, "u idiot, try it yourself before asking, nob!" and I would try it myself, if the wiki taught it more clearly. But while the wiki is good for some things, when I looked up DataStore it was very confusing, and I did not understand it.

Thanks!

1 answer

Log in to vote
1
Answered by 10 years ago

NOTE: Currently, I only know about Setting and Retrieving data. Therefore, I don't know everything about Data Stores. Sorry for the inconvenience.

Accessing Data Stores

We use "DataStoreService" to access Data Stores. for example:

DSS = game:GetService("DataStoreService")

DATA STORES MUST ONLY BE USED IN SERVER SCRIPTS. What Are Data Stores? Imagine a drawer, as a player. The Drawer would have a lot of files. Thise Files would be called the Data Stores. In those files, are 2 columns. A Key and a Value. You use a Key to gain/set data. There is a limit, please read the wiki for more info.

Getting Data Stores First, you have to get the data store, before you can do anything else. We use :GetDataStore(name) to do that.

If the Data Store hasn't been created yet, ROBLOX will automatically create a new one.

Getting Values In this case, we use :GetAsync(key). This gives us the value of the key in the parenthesis. Example: print(DSS:GetDataStore(player.Name.."'s Random"):GetAsync(Random))--Output will print whatever the values are for that key.

If there is no key/value, it will return nil Setting Values In order to get values, you need to set one. To set a NEW value, you would use :SetAsync(key,value). Example:

DSS:GetDataStore(player.Name.."'s Random"):SetAsync("LOL", true) -- Creates a new row in the 2 columns and sets the value to true.

Example Script This script will save the player's Cash if it has not been saved before.

player = game.Players.Player1 --Declares a player
DSS = game:GetService("DataStoreService")--Declare the Data Store Service
PlayerStore = DSS:GetDataSotre(player.Name.."'s DataStores") 
if PlayerStore:GetAsync(player.Name.."'s stats") == nil then --Uses :GetAsync to find out if the stat hasn't been saved before 
PlayerStore:SetAsync(player.Name.."'s stats",player.leaderstats.Cash.Value) --Saves the Cash.
end
0
So what's the difference between a key and a value? Why do you keep doing the player.Name.."'s Random" thing? What does the LOL and true do for SetAsync? The wiki uses UpdateAsync instead, is that better? Thanks. ZeptixBlade 215 — 10y
0
Keys are Strings and values can be anything. the VALUE can change but the KEY CANT. The Key is basically a code to identify a value. UpdateAsync is used to update a Key's value. It is strongly suggested to use UpdateAsync if you know that the Key already exists. LOL and true are just examples. LOL is the key, and true is the value. fahmisack123 385 — 10y
Ad

Answer this question