Issue with saving player data

  1. What do you want to achieve?
    I want to save player data, so that players can continue from where they left off when they rejoin the game.

  2. What is the issue?
    I wrote a script to save player data but it doesn’t save anything

  3. What solutions have you tried so far?
    I have checked the script for any errors, and I have also looked for solutions on the Developer Forum, but I have not found anything that solves my problem, I tried tutorials but nothing helped.

My script

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DataStore")

local function saveData(player)

	local tableToSave = {
		player.leaderstats.Turtles.Value;
		player.leaderstats.Cursor.Value;
		player.leaderstats.TurtlesPS.Value
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave)
	end)

	if success then 
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)
	end
end

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

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

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

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

	local data
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore

	end)

	if success and data then -- If there were no errors and player loaded the data

		Turtles.Value = data[1]
		Cursor.Value = data[2]
		TurtlesPS.Value = data[3]

	else
		print("The player has no data!")
		Cursor.Value += 1
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err  = pcall(function()
		saveData(player)
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function()
	task.wait(5)
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			saveData(player)
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)
2 Likes

Hi,

First you need to make sure the HTTP service is enabled and that the data saved are values

The problem i think is when you load the data tho

Instead of having the tablename[tableIndentaiont]
you should do data.Turtles

2 Likes

Hi,

I have enabled HTTP service and the data saved is values, I tried using data.Turtles instead of data.[tableIndentaiont], but it didn’t help.

1 Like

There is a tutorial on this topic if you wnat :smiley:

Make sure it is inside a PCALL function and make it so that it prints the error message
then plesae send me the error code

local success, errormessage = pcall(function()
  local data = dataSotre:getAsync()
end)
if success then
  ""Code here <==
else
  print "Data not loaded because :"..errormessage
end

1 Like

I’ve already watched this tutorial, script doesn’t print “Data not loaded because :”…errormessage.

Did you test this in a live server and not in Studio?

Wait is the problem happening Inside the LOADING or the SAVING part of the script ?

If it is please wrap the loading AND the saving of the data in a pcall or troubleshot it

I dont think it changes anything

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DataStore")

local function saveData(player)

	local tableToSave = {
		player.leaderstats.Turtles;
		player.leaderstats.Cursor;
		player.leaderstats.TurtlesPS
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave)
	end)

	if success then 
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)
	end
end

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

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

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

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

	local data
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore

	end)

	if success and data then -- If there were no errors and player loaded the data

		Turtles.Value = data.Tutles
		Cursor.Value = data.Cursor
		TurtlesPS.Value = data.TurtelPS

	else
		print("The player has no data!")
		Cursor.Value += 1
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err  = pcall(function()
		saveData(player)
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function()
	task.wait(5)
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			saveData(player)
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

I have tested it in roblox studio by clicking on the test button, and in roblox

I don’t know, it just prints (“Data has been saved”!)

local tableToSave = {
		player.leaderstats.Turtles.Value; 
		player.leaderstats.Cursor.Value;
		player.leaderstats.TurtlesPS.Value
	}

Try splitting the arrays using commas only and not commas and dots.

I tried this, but it still doesn’t work

See if the data is really saving, make a line where it prints the data like in your case Use print(data) if I’m not wrong

Hi there,

While your original code you provided is tentatively just fine, I’ve patched up a few issues and redundancies with some of the previously posted codes.

The fixes mainly contain changes to your method of “calling” data from the array you’re saving, redundancies surrounding unnecessary pcall()s and data formatting. Despite your original data structure being just fine, give the below code a shot and report back on any issues you may still encounter.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DataStore")

local function saveData(player)
	local leaderstats = player:FindFirstChild("leaderstats")

	if leaderstats then
		local Turtles, Cursor, TurtlesPS = leaderstats:FindFirstChild("Turtles"), leaderstats:FindFirstChild("Cursor"), leaderstats:FindFirstChild("TurtlesPS")

		if Turtles and Cursor and TurtlesPS then
			local tableToSave = {
				["Turtles"] = Turtles.Value,
				["Cursor"] = Cursor.Value,
				["TurtlesPS"] = TurtlesPS.Value
			}

			local success, err = pcall(function()
				dataStore:SetAsync(player.UserId, tableToSave)
			end)

			if success then 
				print("Data has been saved!")
			else
				print("Data hasn't been saved due to error: "..err)
			end
		end
	end
end

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

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

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

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

	local data
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
	end)

	if success and data then -- If there were no errors and player loaded the data
		Turtles.Value = data["Turtles"]
		Cursor.Value = data["Cursor"]
		TurtlesPS.Value = data["TurtlesPS"]
	else
		print("The player has no data!")
		Cursor.Value += 1
	end
end)

Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		saveData(player)
	end
end)

If none of the provided code revisions work for you, the problem may also lie in your method of “updating” the data. How exactly are you updating the Turtles, Cursor, and TurtlesPS data? Where are these values being modified? From the looks of it, it seems that you may be updating these values on the client side and expecting the server to be aware of these changes and save them accordingly, which will never work. While testing your datasaving method, always make sure that all data changes occur on the server, and not from the client (in Roblox Studio’s client view, or through a LocalScript).

If kept all of the above points in mind, you shouldn’t run into issues any further.

2 Likes

Hi,

I tried this code and it helped, thanks everyone for help! :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.