Saved data don't load

I can’t understand - i test this code and all works fine - but stats don’t save and load correctly i think.
VIDEO

local dataStoreService = game:GetService("DataStoreService")
local clickDataStore = dataStoreService:GetDataStore("Clicks")
local rebirthsDataStore = dataStoreService:GetDataStore("Rebirths")
local gemsDataStore = dataStoreService:GetDataStore("Gems")

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

	local click = Instance.new("IntValue", leaderstats)
	click.Name = "Clicks"
	click.Value = 0

	local gems = Instance.new("IntValue", leaderstats)
	gems.Name = "Gems"
	gems.Value = 0

	local rebirth = Instance.new("IntValue", leaderstats)
	rebirth.Name = "Rebirths"
	rebirth.Value = 0

	local playerUserId = "player_"..player.UserId

	--Loading Click Data--
	local clickData
	local success, errormessage = pcall(function()
		clickData = clickDataStore:GetAsync(playerUserId)
	end)
	if success  then
		click.Value = clickData
	end

	--Loading Rebirths Data--
	local rebirthsData
	local success, errormessage = pcall(function()
		rebirthsData = rebirthsDataStore:GetAsync(playerUserId)
	end)
	if success  then
		rebirth.Value = rebirthsData
	end

	--Loading Gems Data--
	local gemsData
	local success, errormessage = pcall(function()
		gemsData = gemsDataStore:GetAsync(playerUserId)
	end)
	if success  then
		gems.Value = gemsData
	end
end)

--Saving Data--
game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "player_"..player.UserId

	--Saving Clicks--
	local clickValue = player.leaderstats.Clicks.Value

	local success, errormessage = pcall(function()
		clickDataStore:GetAsync(playerUserId, clickValue)
	end)

	--Saving Rebirths--
	local rebirthsValue = player.leaderstats.Rebirths.Value

	local success, errormessage = pcall(function()
		rebirthsDataStore:GetAsync(playerUserId, rebirthsValue)
	end)

	--Saving Gems--
	local gemsValue = player.leaderstats.Gems.Value

	local success, errormessage = pcall(function()
		gemsDataStore:GetAsync(playerUserId, gemsValue)
	end)
end)

game:BindToClose(function(player)
	for _, Player in pairs(game.Players:GetPlayers()) do
		local playerUserId = "player_"..player.UserId

		--Saving Clicks--
		local clickValue = player.leaderstats.Clicks.Value

		local success, errormessage = pcall(function()
			clickDataStore:GetAsync(playerUserId, clickValue)
		end)

		--Saving Rebirths--
		local rebirthsValue = player.leaderstats.Rebirths.Value

		local success, errormessage = pcall(function()
			rebirthsDataStore:GetAsync(playerUserId, rebirthsValue)
		end)

		--Saving Gems--
		local gemsValue = player.leaderstats.Gems.Value

		local success, errormessage = pcall(function()
			gemsDataStore:GetAsync(playerUserId, gemsValue)
		end)
	end
end)

Hey!
I do believe I’ve found the issue.
When you are saving the different values, you’re using GetAsync. This should only be used when loading. For saving you should use SetAsync.

Hope this helps! :slight_smile:

2 Likes

Omg… XD
I’m so blind. Thx for finding issue!

No problem! Glad I could help! :smiley:

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