Table wont save to datastore

  1. What do you want to achieve? I want to try and save a table to datastore.

  2. What is the issue? 1. I have never worked with tables before interms of datastores. 2. I have no idea how to script one.

  3. What solutions have you tried so far? I have looked around the dev forum and haven’t found anything that helped.

Here is the code I have tried based on my knownledge on saving to datastores

local MDS = game:GetService("DataStoreService"):GetDataStore("StatsIGuess")

game.Players.PlayerAdded:Connect(function(plr)
	local Folder = Instance.new("Folder",plr)
	Folder.Name = "leaderstats"
	
	local IntValue1 = Instance.new("IntValue",Folder)
	IntValue1.Name = "Money"
	
	local IntValue2 = Instance.new("IntValue",Folder)
	IntValue2.Name = "Diamonds"
	
	local IntValue3 = Instance.new("IntValue",Folder)
	IntValue3.Name = "Area"
	
	local ID = plr.UserId.."-Values"
	
	local savedData
	pcall(function()
		savedData = MDS:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		IntValue1.Value = table.find(savedData,nil,1)
		IntValue2.Value = table.find(savedData,nil,2)
		IntValue3.Value = table.find(savedData,nil,3)
	else
		IntValue1.Value = 0
		IntValue2.Value = 0
		IntValue3.Value = 0
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local ID = plr.UserId.."-Values"
	
	pcall(function()
		MDS:SetAync(ID,{
			plr.leaderstats.Money.Value,
			plr.leaderstats.Diamonds.Value,
			plr.leaderstats.Area.Value,
		})
	end)
end)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v then
			v:Kick()
		end
	end
	
	wait(5)
end)

I am mainly requesting help on the game.Players.PlayerRemoving function

1 Like

Change this part:

To this:

IntValue1.Value = savedData[1]
IntValue2.Value = savedData[2]
IntValue3.Value = savedData[3]

If you need any more help let me know, good luck :slight_smile:

Still didnt work. And yes i have API requests allowed.

1 Like

The issue is it isnt saving. I can verify this by using the DataStore editor plugin.

1 Like

I just noticed. Change this line:

To this line:

MDS:SetAsync(ID, {

Your are missing the s as said above ^

I already fixed that upon looking at your reply but it still doesnt work.

1 Like

Are there any errors in output?

From what I can tell no. Im going to add some prints through out my script to see if it randomly stops anywhere

If there are no errors and API services are enabled that is strange. I will look through the code again

Just asking since you didn’t clarify this in your post. You aren’t testing in studio right? Datastores don’t normally work in studio.

1 Like

You can set API services to be enabled and they will work in studio. Do it in game settings.

Ok it works perfectly fine getting the values. But when it goes to save them it doesnt work.

Yes they work in studio. However, sometimes according to a few accounts. The first few times you test datastores in studio the datastores may not save data. However these are just from a few people.

1 Like

You did put the s in there right? It appears to be fine. I can load up a world and test around to see what the issue may be.

Yes.
Here is the current script so you can go test it if you want to

local MDS = game:GetService("DataStoreService"):GetDataStore("StatsIGuess")

game.Players.PlayerAdded:Connect(function(plr)
	local Folder = Instance.new("Folder",plr)
	Folder.Name = "leaderstats"
	
	local IntValue1 = Instance.new("IntValue",Folder)
	IntValue1.Name = "Money"
	
	local IntValue2 = Instance.new("IntValue",Folder)
	IntValue2.Name = "Diamonds"
	
	local IntValue3 = Instance.new("IntValue",Folder)
	IntValue3.Name = "Area"
	
	local ID = plr.UserId.."-Values"
	
	local savedData
	pcall(function()
		savedData = MDS:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		IntValue1.Value = savedData[1]
		IntValue2.Value = savedData[2]
		IntValue3.Value = savedData[3]
	else
		IntValue1.Value = 0
		IntValue2.Value = 0
		IntValue3.Value = 0
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local ID = plr.UserId.."-Values"
	print(1)
	
	pcall(function()
		print(2)
		MDS:SetAsync(ID, {
			plr.leaderstats.Money.Value,
			plr.leaderstats.Diamonds.Value,
			plr.leaderstats.Area.Value,
		})
		print(3)
	end)
	print(4)
end)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v then
			v:Kick()
		end
	end
	
	wait(5)
end)
1 Like

Hi I just wanted to let you know that you should use metatables to add some contingency plans just in case you can’t retrieve data in the table from your datastores. I understand it isn’t working currently but just for the future add a metatable. These allow you to return default values just in case you can’t get something from a table. I use metatables in my games for tables I save. When I can’t get the amount of coins from a player’s table that was saved because the data didn’t load properly, I use a metatable to return a default amount which is 0. Another example of me using a meta table is when a player first joins a game, the game can’t get values from the datastores since there is no saved value in the datastores yet. So what I do is I set a metatable to give players default values for stuff such as coins, levels, and XP.

Your code does save data and it loads up completely fine. Are you sure you have API services enabled? Go to game settings, Security and then Enable Studio Access to API Services.

yes -.- I have API Services enabled. As it gets the data perfectly fine on my side

Ok, you are testing outside of studio? Try testing outside of studio.