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
6 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.

-- for example
if --(datastorevalue) -- lets just say this is coins
> 30 then
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.

2 answers

Log in to vote
0
Answered by 6 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.

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players") 

local DataStore = DataStoreService:GetDataStore("Gold")

Players.PlayerAdded:Connect(function(Player)
    local Key = Player.Userid
    local PlayerGold = DataStore:GetAsync(Key) 
-- This script assumes you've already saved the data of the player.

    if PlayerGold > 100 then
        -- Do something
    end
end)

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
6 years ago

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

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

game.Players.PlayerAdded:connect(function(player)
    local GetData = DataStore:GetAsync(player.userId)
    if GetData[1] then
        if GetData[1] >= 30 then
            --Whatever you want to happen
        end
    end
end)

Answer this question