Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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!

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.