How to check the value of an object in a DataStore? (Tables)

Hello, so I have a DataStore which saves some data; code below if you would need it for some reason.

DataStore Script
-- // Information
local RoadData = {}

-- // Script

while wait(35) do
	for _,Roads in pairs(RoadFolder:GetDescendants()) do
		if Roads:IsA("StringValue") then
			RoadData[Roads.Parent.Parent.Name] = Roads.Value
		end
	end

	local SuccessMessage, ErrorMessage = pcall(function()
		RoadSimDataStore:SetAsync("RoadData", HttpService:JSONEncode(RoadData))
	end)


	if ErrorMessage then
		warn(ErrorMessage)
	end

	if SuccessMessage then
		warn("Data has saved successfully.")
	end
end

game:BindToClose(function()
	if RunService:IsStudio() then
		warn("Data has not been saved: Studio Shutdown")
		return
	end
	
	warn("Data is currently saving.")
	
	for _,Roads in pairs(RoadFolder:GetDescendants()) do
		if Roads:IsA("StringValue") then
			RoadData[Roads.Parent.Parent.Name] = Roads.Value
		end
	end

	local SuccessMessage, ErrorMessage = pcall(function()
		RoadSimDataStore:SetAsync("RoadData", HttpService:JSONEncode(RoadData))
	end)

	if ErrorMessage then
		warn(ErrorMessage)
	end

	if SuccessMessage then
		warn("Data has saved successfully, proceeding with shutdown.")
	end
end)

But my problem is, I have no idea how to check the data inside my DataStore, I do have experience but I’ve never used tables with DataStores.

Here is my code currently;

local RoadFolder = game:GetService("Workspace"):FindFirstChild("Roads")

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

local RoadData = {}

Players.PlayerAdded:Connect(function(Player)
	for _,Roads in pairs(RoadFolder:GetDescendants()) do
		if Roads:IsA("StringValue") then
			Roads.Value = RoadSimDataStore:GetAsync("RoadData", Roads.Parent.Parent.Name [[value]])
		end
	end
end)

This will loop through my folder in workspace,

As you can see in my DataStore I have the values in the table set to the names of roads in my roads folder. I want to get data from the DataStore by looping through my roads folder and setting the Owner value of each road equal to the data inside my DataStore.

Sorry if this is confusing.

Any help would be appreciated, thanks!

Okay, using GetASync repeatedly like that is not good. Try saving a table into the datatstore and retrieving it then going through that table.
‘Table=DataStore:getAsync(keyname,Player)`
for i,v in next, Table do
–Code here
end’
it’s just some pseudo code

That’s the problem, no idea how to retrieve the data from the table. I am already saving a table in the DataStore.

If I’m not mistaken, if you just use the key as the only parameter when using GetAsync, it should give you the whole table.

So,

local data

data = RoadSimDataStore:GetAsync("RoadData") 

—data is the whole table

My question is… are these folders parented under players

Try doing

local data
local s , e = pcall(function() -- Wrapping in pcall to prevent Datastore errors breaking the script
     data = RoadSimDataStore:GetAsync("RoadData") -- Getting data from key 
end)
print(data) -- Now you can see the contents of the table then you can update your code to retrieve a specific value from the table.

The folder is not parented under players, it is in workspace, also I do not want the whole table, I want 1 value from the table.

How would I update my code to retrieve a specific value?

If I’m inderstanding correctly, you’re trying to index a table for a specific value contained within the table? You can index tables from DataStores as you would index any normal table, by using a certain entry’s key.

Going by your image, data would look like this when JSONDecoded:

RoadData = {
    ['Road5'] = '';
    ['Road6'] = '';
    ['Road4'] = '';
    ['Road2'] = '';
    -- Keeps on carrying on like that.
}

Now let’s say that Road5 had a value of ‘1234’, your table would now look like:

RoadData = {
    ['Road5'] = '1234'
    -- other table info
}

If you’re trying to index RoadData’s Road5 value, you have to index it like this:

local road5Data = RoadData['Road5'] -- assigns 1234 as the value
-- OR
local road5Data = RoadData.Road5 -- doesn't make a difference

You can do something like
local Value = data[Key] Key can be an Index or a key value.
Example : data[1] or data['Hi']

Hmm, I think that was what I was looking for, I’ll check it out!

I think your on the right track, on my picture of the DataStore I have a whole table of values, I want to get the value of a certain object in the table and then set the value of it to the string value named Owner.

Ah okay, then you’d just override the value using that specific index like this:

local myTable = {
    ['Rank'] = 'Guest';
    ['Money'] = '1234';
    ['RoadData'] = '1234';
}

myTable.Rank = 'Owner' 
myTable.Money = '9999'
--[[ outputs:
    local myTable = {
    ['Rank'] = 'Owner';
    ['Money'] = '9999';
    ['RoadData'] = '1234';
}
]]

I’m a little confused. May you please explain the purpose of the initial script.

So I am just attempting to retrieve data from my DataStore.

No no I meant how is the data being used lol. Like are you assigning these values to a player or setting a value. When I say assigning I mean like assigning a player ownership to something.

There may be an easier way to write this script depending on how you are handling these values.

I am assigning these values to a StringValue inside an object. Mostly to check if the object already has an owner, and to ID the owner of the object.

Ok from my understanding, you are assigning a road to a player based on previous data. If this is correct then you can just,

Parent a string value to the player,
Change the value to whatever the road name is,

Save the string data in a data store specifically made for players.

So when the player hops back into the game, it’s an easy check and easy assign.

(If another person has the same road already owned , just assign the joining player a new road)

local data = {
RoadName = player.RoadName.Value
}

local SuccessMessage, ErrorMessage = pcall(function()
		RoadSimDataStore:SetAsync(PlayerKey, Data)
end)






local data
local s , e = pcall(function()
     data = RoadSimDataStore:GetAsync(PlayerKey)
end)

player.RoadName.Value = data.RoadName

Sorry that im editing a lot. I’m on my phone and trying to remember things from the top of my head lol

Basically removed the JSONEncode from my DataStore saving stuff, and then used this when retrieving data, thanks!

Basically removed the JSONEncode from my DataStore saving stuff, and then used this when retrieving data, thanks!

1 Like