Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

DataStore isn't Saving Data?

Hello, Dear Pepole! I need help with DataStore, cause Of My Data not Storing Other Players Data, and i Need Help to Store it, I Hope i will Get a Answer!

Script in ServerScript Service:


--[[
	2/2/21
	RektwayYTB
	DataStore/Leaderstats
--]]

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("playerData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Parent = leaderstats
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
	
	local data 
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.UserId.."-points") 
		data = playerData:GetAsync(player.UserId.."-wins")
	end)
	
	if success then 
		points.Value = data
		points.Value = data
	else
		print("Error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		playerData:SetAsync(player.UserId.."-points", player.leaderstats.Points.Value)
		playerData:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving the data")
		warn(errormessage)
	end
	
end)

If i did an Error Please also Answer!

1 answer

✓ Accepted answer

Reason for you problem is that at line 26 you are setting data to data that GetAsync returns, not a problem, but then your are setting the same variable data to new GetAsync on line 27 which overrides previous value. Next is that on line 32 and 33 you set Points value but not wins value.

Your solution would be saving tables, that way you will only need one GetAsync, here is a video tutorial how to save tables and here is simple example of it:

local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('cat')

local DataToSave = {
	wins = 0,
	points = 0,
}

playerData:SetAsync(player.UserId, DataToSave)

-- 1 week later the gamer joins again

local RestoredData = playerData:GetAsync(player.UserId)

print(RestoredData)
--[[ Output:
{
	["wins"]: 0,
	["points"]: 0,
}
--]]

One more possible problem is that you are not saving data on BindToClose, make sure to bind function to it because PlayerRemoving fires when player leaves and then saves data but if game closes before the data is saved, nothing will save, that's why you use game:BindToClose, it will wait until binded function is complete and then only close the game, so you also put saving function in there.

The one more bad practice in your code is SetAsync, consider switching to UpdateAsync, in your case it is very easy to implement.

It's 1/2/21 btw

Sources


UpdateAsync

Youtube tutorial on tables in DataStores

Random post on saving tables in DataStores

1 comment

1

Thank You! Have happy new year

This discussion is preserved as it was. New answers, comments, and votes are closed.