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