I am trying to make it where if a certain value on a DataStore is true, then something will happen to a player. Once again, the wiki isn't really clear on this.
How would I make an if script check the DataStore and function off of it.
1 | -- for example |
2 | if --(datastorevalue) -- lets just say this is coins |
3 | > 30 then |
4 | end |
I'm struggling at the part where it reads the data store. I know this is a pretty easy question but I just can't figure out how to call the value of the DataStore.
Datastores are like a giant table and a way to do this is to get the key of the Player you're looking to check the data for then use the GetAsync method of the Datastore service to get the data from a certain datastore of a certain key.
01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 | local Players = game:GetService( "Players" ) |
03 |
04 | local DataStore = DataStoreService:GetDataStore( "Gold" ) |
05 |
06 | Players.PlayerAdded:Connect( function (Player) |
07 | local Key = Player.Userid |
08 | local PlayerGold = DataStore:GetAsync(Key) |
09 | -- This script assumes you've already saved the data of the player. |
10 |
11 | if PlayerGold > 100 then |
12 | -- Do something |
13 | end |
14 | end ) |
For more information on datastores check the ROBLOX Wiki for it.
http://wiki.roblox.com/index.php?title=API:Class/DataStoreService
Good luck!
Pretty simple, just use :GetAsync to grab the value, for example
01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( "DataStore" ) |
02 |
03 | game.Players.PlayerAdded:connect( function (player) |
04 | local GetData = DataStore:GetAsync(player.userId) |
05 | if GetData [ 1 ] then |
06 | if GetData [ 1 ] > = 30 then |
07 | --Whatever you want to happen |
08 | end |
09 | end |
10 | end ) |