How to get a specific value from a DataStore table

Trying to get a specific value from a players data, I’ve accessed the player data using GetAsync but I Icant get a specific value out of it.
This is a picture of the datasave script showing that I used tables.
image

How would I get my desired value out of the table, it keeps returning as nil.

Any help is appreciated!

2 Likes

If I were you, I would give your data a string index.

So instead of using dataToSave[i], try using dataToSave[v.Name] which will create an index with the folder’s name. Then when you are ready to index your data, you can do dataToSave['myDataIndex']. This way, it stays the same and you’re able to index it the same way each time.

1 Like

Okay, I understand that now, but how do I use SetAsync in this case.

2 Likes

For example,

for i, folders in pairs(folder:GetChildren()) do
    dataToSave[folders.Name] = {}
    for j,data in pairs(folders:GetChildren()) do
        dataToSave[folders.Name][data.Name] = data.Value
    end
end
2 Likes

So it would be, datasave:SetAsync(plr.UserId,dataToSave)?

2 Likes

Yeah, exactly. You can save dictionaries to DS just as you’re able to save normal tables.

2 Likes

So making an index will reference every value inside the table and I will be able to choose which of the values I want to change basically?

1 Like

Yeah, exactly. So if your data looks like

local myData = {
    ['Colours'] = {
        ['R'] = 123;
        ['G'] = 123;
        ['B'] = 123;
    };
}

You could just do

print(myData['Colours']['R']) -- should output 123
2 Likes

Alright, I’ll attempt this and see if it works, ill update you on it when I can!

2 Likes

So, I came across an error.
image

image

1 Like

Hmmm, could you send what your player folder looks like?

1 Like

Here you go.
image

1 Like

Thanks! And that highlighted line is the line that’s erroring, correct?

1 Like

Yes it is. Tried fixing but I don’t understand how to

1 Like

Do you have the new expressive output? If so, could you print the saves variable?

1 Like

Did when I left the game. image

1 Like

Thanks! I think I see the issue, hold on.
So, you’d have to try something like this:

for i,folder in pairs(Storage:GetChildren()) do
    local dataFolder = Instance.new('Folder', PlrStats)
    dataFolder.Name = folder.Name
    for j,value in pairs(folder:GetChildren()) do
        local dataValue = Instance.new('IntValue', folder)
        dataValue.Name = Value.Name
        dataValue.Value = saves[i][value.Name] -- indexes your saved data with the name of the value inside of your folder inside of storage. 'i' is your folder's name, and 'j' is the specific value's name.
    end
end
1 Like

Gives the same error, Its trying to get the health value from something that doesn’t exist, I’ll try to use what you said to fix the old Datasave script to see if that works.

2 Likes

Okay I fixed it the problem now I am trying to figure out how to get the data from another script. Is it like this?
image

1 Like

Yeah, that should work as long as myData[‘Guild’][‘Member’] exists.

1 Like