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

How would I check the value if a DataStore in an "if, then" statement?

Asked by
Dad_Bot 34
7 years ago

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
2if --(datastorevalue) -- lets just say this is coins
3> 30 then
4end

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.

2 answers

Log in to vote
0
Answered by 7 years ago

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.

01local DataStoreService = game:GetService("DataStoreService")
02local Players = game:GetService("Players")
03 
04local DataStore = DataStoreService:GetDataStore("Gold")
05 
06Players.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
14end)

For more information on datastores check the ROBLOX Wiki for it.

http://wiki.roblox.com/index.php?title=API:Class/DataStoreService

Good luck!

Ad
Log in to vote
0
Answered by
dirk2999 103
7 years ago

Pretty simple, just use :GetAsync to grab the value, for example

01local DataStore = game:GetService("DataStoreService"):GetDataStore("DataStore")
02 
03game.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
10end)

Answer this question