Saved data not loading. How do I fix?

Like this?

spawn(function()
		while wait(60) do
			local success, error = pcall(function()
				ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)	
			end)

			if success then
				print("Data auto saved")
			else
				print("There was an error when saving data")
				warn(error)
			end
		end
	end)

you shouldnt put a while loop in there and if you are going to loop save the data for whatever reason use a “for loop”, the “while wait()” idiom is not good lol

How do I make it only save until the player is in the game. So that it doesn’t self wipe the data?

Would I just change the while with for?

Or should it be like this:

local KeepMyLoopRunning[Player] = {}
if KeepMyLoopRunning[Player] == nil then KeepMyLoopRunning[Player] = true end
while true do
	wait(60)
	if KeepMyLoopRunning[Player] = true then
		spawn(function()
				local success, error = pcall(function()
					ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)	
				end)

				if success then
					print("Data auto saved")
				else
					print("There was an error when saving data")
					warn(error)
				end
			end
		end) 
	else
		Break
	end
end

game.Players.PlayerRemoving:Connect(function(Player)
	KeepMyLoopRunning[Player] = false
end)

i havent read everyones replys so i dont know if this is fixed yet but A):
you havent defined the datastore service at the start of the script

local DSS = game:GetService("DataStoreService")

and B) there is no reason for a while loop in this sciprt so heres how i would change your original script post: also i have just figured out that because of that while loop you are setting things in the data store every 60 seconds which could cause the game to throttle your data store requests if its in a server script so id remove that and just have data set when a player leaves and then fetched when the player comes back

local DSS =game:GetService("DataStoreService")
local ClickDataStore = DSS:GetDataStore("ClickDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = player
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = stats
	
	local data
	local success, errormessage = pcall(function()
		data = ClickDataStore:GetAsync(player.userId.."Clicks")
	end)
	if success then
		Clicks.Value = data
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage =  pcall(function()
		ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

not quite, when a player joins the loop begins possibly for each individual player creating a load of while true loops running for multiple different clients on the server when they join so the method is different but in the end yes its performance drops that will result

Ok. I have tried this just now. But when I Join the game it doesn’t load the saved data. I’m not sure if its because its not saving or if its just not loading.

have u published the game and are joinning it as the actual published game or are u joinning it in studio and are you varaibles all correct?

i also just spotted a big problem wait nvm

I am joining the actual game that has been published.

run this and show me ur output also make sure u do a few clicks and you are running it in studio

local DSS =game:GetService("DataStoreService")
local ClickDataStore = DSS:GetDataStore("ClickDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local Stat = Instance.new("Folder")
	Stat.Name = "leaderstats"
	Stat.Parent = player
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = Stat
	
	local data
	local success, errormessage = pcall(function()
		data = ClickDataStore:GetAsync(player.userId.."Clicks")
	end)
	if success then
		Clicks.Value = data
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	print(player.leaderstats.Clicks.Value)
	local success, errormessage =  pcall(function()
		ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
	end)
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)
  12:32:58.065  Player Data successfully saved!  -  Server  -  Script:18
  12:33:07.974  Infinite yield possible on 'cloud_218303841.Model.GUIs.SignalGUI.Scripts:WaitForChild("Mode")'  -  Studio
  12:33:07.974  Infinite yield possible on 'cloud_218303841.Model.GUIs.SignalGUI.Scripts:WaitForChild("Mode")'  -  Studio
  12:33:09.791  Infinite yield possible on 'cloud_218303841.Model.GUIs.SignalGUI.Scripts:WaitForChild("Mode")'  -  Studio
  12:33:09.791  Infinite yield possible on 'cloud_218303841.Model.GUIs.SignalGUI.Scripts:WaitForChild("Mode")'  -  Studio
  12:33:18.198  New tag editor version coming online; unloading the old version  -  Studio
  12:33:18.352  0  -  Server  -  Script:26
  12:33:18.367  New tag editor version coming online; unloading the old version  -  Studio
  12:33:18.351  Disconnect from ::ffff:127.0.0.1|64702  -  Studio

That is the output. But it still didn’t work.

studio can be a bit buggy lemme just write some more print checks into the script your gonna have to run the studio a few times for it to work maybe also do u have this turned on:

I already have that turned on.

i have tested it, this is the fundementals of ur script
part one (run this chunk in command bar)

local DSS =game:GetService("DataStoreService")
local ClickDataStore = DSS:GetDataStore("ClickDataStoreBOABDOSB310923890218392")
print("gonna save this", 32)

local success, errormessage =  pcall(function()
	ClickDataStore:SetAsync("73373363".."Clicks", 32)
end)
if success then
	print("Player Data successfully saved!")
else
	print("There was an error when saving data")
	warn(errormessage)
end

part 2 (run this in your command bar)

local DSS =game:GetService("DataStoreService")
local ClickDataStore = DSS:GetDataStore("ClickDataStoreBOABDOSB310923890218392")
local data
local success, errormessage = pcall(function()
	data = ClickDataStore:GetAsync("73373363".."Clicks")
end)
if success then
	print("Player Data successfully saved!")
	print("this is our new data:", data)
else
	print("There was an error when saving data")
	warn(errormessage)
end

as u can see it outputs the correct test number of 32

now the only possible error i can think of is that when u are calling

this data value might already be gone because it is stored in player perhaps if you store it under Players and set the name to something like (“leaderstats”…player.Name) then destroying it when the player leavesand ill write the code out for that now but after that theres nothing more wrong with the code i can see

These are my leaderstats:

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

	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Value = 0
	Clicks.Parent = stats

	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Value = 0
	Gems.Parent = stats

	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 0
	Rebirths.Parent = stats

	local Eggs = Instance.new("IntValue")
	Eggs.Name = "Eggs"
	Eggs.Value = 0
	Eggs.Parent = stats
end)

Should I delete them as they are created by the data store

heres the final version of the code i have tested that it works but youll probably have to test it in the published game because datastores dont really work in studio but i have tested the scripts fundamentals and they work

local DSS =game:GetService("DataStoreService")
local ClickDataStore = DSS:GetDataStore("ClickDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local Stat = Instance.new("Folder")
	Stat.Name = "Leaderstats"..player.Name
	Stat.Parent = game.Players
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = Stat

	local data
	local success, errormessage = pcall(function()
		data = ClickDataStore:GetAsync(player.userId.."Clicks")
	end)
	if success then
		Clicks.Value = data
		print("Player Data successfully saved!")
		print("this is our new data:", data)
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	print("gonna save this", player.leaderstats.Clicks.Value)
	
	local success, errormessage =  pcall(function()
		ClickDataStore:SetAsync(player.UserId.."Clicks",game.Players["LeaderStats"..player.Name].Clicks.Value)
	end)
	if success then
		print("Player Data successfully saved!")
		game.Players["LeaderStats"..player.Name]:Destroy()
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

with the other values you’ll have to copy what ive done with the click value (where i have saved it to and how i have destroyed it ect)

and for good measure heres me testing the funadamentals:
saving to the data store with a test number of 32:
image
output:
image

calling the data:
image
output:
image

2 Likes