DataSave Not Working

DataSave script for time isn’t working and I don’t know why. No errors in the output. Here’s the script:

local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Time = Instance.new("IntValue")
	Time.Name = "Time"
	Time.Parent = leaderstats

	local playerUserId = "Player"..player.UserId

	local data 

	local success, errormessage = pcall(function() print("kill me")
		data = myDataStore:GetAsync(playerUserId) 
	end)


	if success then
		Time.Value = data  
	end

	while wait(1) do
		player.leaderstats.Time.Value = player.leaderstats.Time.Value + 1 
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player"..player.UserId

	local data = player.leaderstats.Time.Value

	myDataStore:SetAsync(playerUserId, data)          

end)
2 Likes

Try using

if success then
 -- Your code

else

  warn(errormessage)

end

Do the same thing for the “PlayerRemoving” event. Once you do so and test it again, tell me if it shows an error.

Is it only not working in studio , or in Roblox as well, what I’ve noticed once while recording a tutorial video, if once you leave the game you do not see a Disconnected message, your data will not save!

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Time = Instance.new("IntValue")
	Time.Name = "Time"
	Time.Parent = leaderstats

	local data 
	local succ, err = pcall(function()
		data = myDataStore:GetAsync("Player"..player.UserId) 
	end)

	if succ then
		if data then
			Time.Value = data
		end
	else
		warn(err)
	end
	
	task.spawn(function()
		while task.wait(1) do
			player.leaderstats.Time.Value += 1
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	local data
	local succ, err = pcall(function()
		data = myDataStore:SetAsync("Player"..player.UserId, player.leaderstats.Time.Value)
	end)
	
	if succ then
		return
	else
		warn(err)
	end
end)

game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		local data
		local succ, err = pcall(function()
			data = myDataStore:SetAsync("Player"..player.UserId, player.leaderstats.Time.Value)
		end)
		
		if succ then
			return
		else
			warn(err)
		end
	end
end)

You may also want to add a function which performs auto-saving every minute.

You shouldn’t auto-save every minute because this can exhaust the data store. Maybe every 5 minutes or a bit longer.

Every minute is fine, check out the DataStore limits section for more info.

https://developer.roblox.com/en-us/articles/Data-store#limits-1

You can technically auto-save every player’s stats every 6 seconds without issue however this is slightly overkill.

I personally only save data when the player leaves and/or the server shuts down, auto-saving was just a possible suggestion.

A button which allows the player to save their progress (and gives them comfort of mind that their stats have been saved) might be a nice addition.

Ah okay. I always thought this because in studio if I save data too frequently it errors. But I do have it wrapped in a pcall so it doesn’t break the game. Does this only happen in studio or does it happen in the real game as well?

pcall() calls are necessary when making any API request, since when API requests fail, errors are raised/thrown, which results in the premature termination of scripts.

1 Like

Thank you so much it works now!!!