Saved data not loading. How do I fix?

The DataStore works but I have no idea on how to load the saved player data when they re-join the game.
This is the datastore:


local ClickDataStore = DataStoreService: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

	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)

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)
5 Likes

I am confused on what you mean by this. The PlayerAdded event should fire and have GetAsync() retrieve your data when a player joins in the game. Check out this devhub on Datastores if you are still confused, it should give you answers on Datastores.

1 Like

I have already read that but still have no idea. I have no experience with scripting.:joy:Thanks for trying to help though.

2 Likes

There are problems with your code:

  1. The while true loop will never stop looping, so it will always save even when the player has left which will cause performance drop.
  2. Sometimes, descendants of the Player will be destroyed before PlayerRemoving fires.

You can also debug this. Print out the variable data to see if there is a data.

1 Like

This?
image

1 Like

How do I do this because I don’t know how.

1 Like

Yes, you never stopped the loop even if the player left the game.

A Roblox method called print which basically shows a text in the Output window of Roblox Studio.

print("Hi") -- in the Output window, you will see "Hi"
1 Like

How do I make it so that it stops looping when they leave the game?

1 Like

Do I do it like this?

print(data)
1 Like

As much I would like to help, it would be better to learn on your own. What I could say is that you don’t need to put a loop there, instead outside of it.

Yes.

1 Like

put the loop inside a coroutine

1 Like

Replace your code with this

local ClickDataStore = DataStoreService: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
        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
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)
1 Like

If you ask most devs whats the most anoiying thing about scripting they usually say Datastores, if you do them right you have little to no issues but that takes alot of checking and making sure things are loaded and doing what they should be doing aside from that since your new I suggest learning basics first since a datastore that won’t eventually wipe peoples data takes a while to lear, that being said your datastore would fail and wipe not save data early on just for the pure fact of you doing a loop that saves every 60 seconds, saving every certain amount of time will overload the datastore max limit and start queing data then it will stop responding so you shouldn’t be autosaving in the way your doing it but to answer your question to stop the loop when they leave you do

local KeepMyLoopRunning[Player] = {}
if KeepMyLoopRunning[Player] == nil then KeepMyLoopRunning[Player] = true end
while true do
wait(60)
if KeepMyLoopRunning[Player] = true then
--Do Something like save data or whatever you want 
else
Break
end
end

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

so your saying if the that value is true then keep running the loop but whenever it’s set to false then you break the loop and make the loop stop running. the [Player] part is so the game knows on the server to only have this value for this individual player and not for everyone so you start it as a table then say if it’s nil wich it starts as nil then to make it equal to true so the loop can start running until you make it false and the loop breaks.

1 Like

image

that means you messed your ends up, you need to have the right end and end) for things and the right amount of them or you get errors like that.

Just got signed out.
image

What is wrong?

local DataStoreService = game:GetService("DataStoreService")
local ClickDataStore = DataStoreService: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
	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)

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)

Any errors? I don’t know. You tell me

This part has one end missing, the with the ) is only end. No )'s.

After that end, then there’s a end).

Ok. I will try this right now.