How to get a specific value from a DataStore table

It doesn’t though, I wonder if I should reference “dataToSave” too.

image
image

1 Like

Could you send your current save script?

–local DataStore = game:GetService(“DataStoreService”)
local gameData = DataStore:GetDataStore(“data 2”)

local Storage = script.Storage:GetChildren()

local maxLevel = 5000

local function saveData(plr, folder)
warn(“Saving Data…”)
local dataToSave = {}

for i, folders in pairs(folder:GetChildren()) do
	--Making SubTables or Tables inside the main table
	dataToSave[i] = {}
	
	for j, data in pairs(folders:GetChildren()) do
		dataToSave[i][j] = data.Value
		print(data.Name, dataToSave[i][j])
	end
end

gameData:SetAsync(plr.UserId.."-dataToSave", dataToSave)
warn("Data Saved!")

end

game.Players.PlayerAdded:Connect(function(newPlr)
local PlrStats = Instance.new(“Folder”,newPlr)
PlrStats.Name = “PlrStats”

--Generating new Data
for i, folders in pairs(Storage) do
	local newFold = folders:Clone()
	newFold.Parent = PlrStats
end

--Looking for saved Data
local saves = gameData:GetAsync(newPlr.UserId.."-dataToSave")

--If the player has saved data
if saves then
	warn("Player Data Found!")
	
	--Loading data 
	for i, folders in pairs(PlrStats:GetChildren()) do
		for j, data in pairs(folders:GetChildren()) do
			data.Value = saves[i][j]
			print(i,j,data.Name,data.Value)
		end
	end
	
--If the player doesn't have saved data
else
	warn("Player Doesn't have Data...")
	
end

while wait() do
	local exp = PlrStats:WaitForChild("PlrInfo"):WaitForChild("Exp")
	local level = PlrStats:WaitForChild("PlrInfo"):WaitForChild("Level")
	local Points = PlrStats:WaitForChild("PlrInfo"):WaitForChild("Points")
	
	local extraLevel = level.Value
	local expNeed = ((extraLevel * 2)-1) * 15
	
	if level.Value < maxLevel then
		if exp.Value >= expNeed then
			
			--Increase level by 1
			level.Value = level.Value + 1
			
			--Remove needed exp
			exp.Value = exp.Value - expNeed
			
			--Add points based on every level
			Points.Value = Points.Value + 2
		end
	end
end

end)

game.Players.PlayerRemoving:Connect(function(oldPlr)
local PlrStats = oldPlr:WaitForChild(“PlrStats”)
saveData(oldPlr,PlrStats)
end)–

1 Like

Is this what you’re using to save the data?

1 Like

Yes and then it fires this function.
image

I just want to know how to print my data from another script by using GetAsync.

2 Likes

Ah, got it. You should be able to print it as normal.

Try doing print(myData). It appears you have the new output so that should be able to print.

1 Like

So it would print all the data by just doing this?
image

2 Likes

Yep, it should print the table.

2 Likes

It prints “nil” its like it wont get the data does it have to do with the datastores
image
image

2 Likes

I see. You have to get the same datastore on both scripts, being either “data 2” or “data 3”. It’s getting two separate datastores with two separate values.

1 Like

Aight I see it now it doesn’t print nil this time but it printed a table with dots “{…}”. Is there any way behind this?

2 Likes

Yep. You have to click on those dots, or there might be an arrow beside it, and you’re able to browse the table.

1 Like

Thank you so much for helping me! Finally understand!
image

2 Likes

I have one last simple question, I tried changing the value using SetAsync and it didn’t change, The GetAsync function works but to change the value and using SetAsync is giving me trouble. I’d appreciate if you’d explain that too and thanks!
Here’s the script.
image

1 Like

Is your key the same amongst all of your scripts?

1 Like

Do you mean the players UserId?

1 Like

When you do :SetASync, the first argument is your key, in your case it would be plr.UserId.."Guild".."Member". Is this the key in all of your DS scripts?

1 Like

I see. Well I don’t use strings with the players data is that the problem, I usually only have the player’s UserId with out a string with it, is the string important for changing the Values in other scripts?

1 Like

Yeah, you’d have to :SetAsync with the entire table, you can’t index and update a specific value like that.

So in the datasave script I should put the strings, in GetAsync and SetAsync, and then I will be able to change it from another script by using that same string?

2 Likes