Why is my datastore not saving?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am attempting to make my datastore save values but it is not.

  1. What is the issue? Include screenshots / videos if possible!

It does not save with no errors printed out.

here is my code

local MainStore = game:GetService("DataStoreService"):GetDataStore("MainStore")

local HttpService = game:GetService("HttpService")

local function SavePlayerData(player)
	local success, err = pcall(function()

		local data = {
			Clicks = player:WaitForChild("leaderstats").Clicks.Value;
			Gems = player:WaitForChild("leaderstats").Gems.Value;
			Rebirths = player:WaitForChild("leaderstats").Rebirths.Value;
			WalkSpeed = player:WaitForChild("HiddenStats").WalkSpeed.Value;
			TotalGems = player:WaitForChild("TotalGems").Value;
			TotalEggs = player:WaitForChild("TotalEggs").Value;
			TotalClicks = player:WaitForChild("TotalClicks").Value;
			TimePlayed = player:WaitForChild("TimePlayed").Value;
			MagmaPortal = player:WaitForChild("MagmaPortal").Value;
			OceanPortal = player:WaitForChild("OceanPortal").Value;
			OasisPortal = player:WaitForChild("OasisPortal").Value;
		}
		
		MainStore:SetAsync("TestPlayer-"..player.UserId, data)
	end)
	
	if success then
		print("saved data")
	else
		print(err)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local data
		
		local success,err = pcall(function()
			data = MainStore:GetAsync("TestPlayer-"..player.UserId) 
		end)
		
		if success  then
			print("loaded player data")
		else
			print(err)
		end
		
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player
	
		local HiddenStats = Instance.new("Folder")
		HiddenStats.Name = "HiddenStats"
		HiddenStats.Parent = player
		
		local coins = Instance.new("IntValue")
		coins.Name = "Clicks"
		if data then coins.Value = data.Clicks else coins.Value = 0 end
		coins.Parent = leaderstats
	
		local DailyRewards = Instance.new("StringValue")
		DailyRewards.Name = "DailyRewards"
		DailyRewards.Value = HttpService:JSONEncode({nil})
		DailyRewards.Parent = HiddenStats
	
		local IsOpening = Instance.new("BoolValue")
		IsOpening.Value = false
		IsOpening.Parent = player
		IsOpening.Name = "IsOpening"
	
		local Island = Instance.new("StringValue")
		Island.Name = "Island"
		Island.Value = "Grassy"
		Island.Parent = player
		
		local Multiplier = Instance.new("IntValue")
		Multiplier.Name = "Multiplier"
		Multiplier.Value = 1
		Multiplier.Parent = player
			
		local gems = Instance.new("NumberValue")
		gems.Name = "Gems"
		if data then gems.Value = data.Gems else gems.Value = 0 end
		gems.Parent = leaderstats
	
		local TotalGems = Instance.new("IntValue")
		TotalGems.Name = "TotalGems"
	 	if data then TotalGems.Value = data.TotalGems else TotalGems.Value = 0 end
		TotalGems.Parent = player
	
		local TotalEggs = Instance.new("IntValue")
		TotalEggs.Name = "EggsOpened"
		if data then TotalEggs.Value = data.TotalEggs else TotalEggs.Value = 0 end
		TotalEggs.Parent = player
		
		local Rebirths = Instance.new("NumberValue")
		Rebirths.Name = "Rebirths"
		if data then Rebirths.Value = data.Rebirths else Rebirths.Value = 0 end
		Rebirths.Parent = leaderstats
	
		local WalkSpeed = Instance.new("IntValue")
		WalkSpeed.Name = "WalkSpeed"
		if data then WalkSpeed.Value = data.WalkSpeed else WalkSpeed.Value = 15 end
		WalkSpeed.Parent = HiddenStats
		
		char.Humanoid.WalkSpeed = WalkSpeed.Value
		WalkSpeed.Changed:Connect(function()
			char.Humanoid.WalkSpeed = WalkSpeed.Value
		end)
		
		local TotalClicks = Instance.new("IntValue")
		TotalClicks.Name = "TotalClicks"
		if data then TotalClicks.Value = data.TotalClicks else TotalClicks.Value = 0 end
		TotalClicks.Parent = player
	
		local TimePlayed = Instance.new("IntValue")
		TimePlayed.Name = "TimePlayed"
		if data then TimePlayed.Value = data.TimePlayed else TimePlayed.Value = 0 end
		TimePlayed.Parent = player
	
		spawn(function()
			while wait(1) do
				TimePlayed.Value = TimePlayed.Value + 1
			end
		end)
		
		local SuperClick = Instance.new("IntValue")
		SuperClick.Name = "SuperClick"
		SuperClick.Value = 0
		SuperClick.Parent = player
		
		local DoubleClick = Instance.new("IntValue")
		DoubleClick.Name = "DoubleClick"
		DoubleClick.Value = 0
		DoubleClick.Parent = player
		
		local SuperClickDB = Instance.new("BoolValue")
		SuperClickDB.Name = "SuperClickDB"
		SuperClickDB.Value = true
		SuperClickDB.Parent = player
	
		local ClickBoost = Instance.new("BoolValue")
		ClickBoost.Name = "ClickBoost"
		ClickBoost.Value = false
		ClickBoost.Parent = player
		
		local GemBoost = Instance.new("BoolValue")
		GemBoost.Name = "GemBoost"
		GemBoost.Value = false
		GemBoost.Parent = player
	
		local MagmaPortal = Instance.new("BoolValue")
		MagmaPortal.Name = "MagmaPortal"
		MagmaPortal.Parent = player
		if data then MagmaPortal.Value = data.MagmaPortal else MagmaPortal.Value = false end
	
		local OceanPortal = Instance.new("BoolValue")
		OceanPortal.Name = "OceanPortal"
		OceanPortal.Parent = player
		if data then OceanPortal.Value = data.OceanPortal else OceanPortal.Value = false end
	
		local OasisPortal = Instance.new("BoolValue")
		OasisPortal.Name = "OasisPortal"
		OasisPortal.Parent = player
		if data then OasisPortal.Value = data.OasisPortal else OasisPortal.Value = false end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	SavePlayerData(player)
end)

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() == false then
		for _,v in pairs(game.Players:GetPlayers()) do
			SavePlayerData(v)
			wait(1)
		end
	end
end)

Testing in studio can cause ROBLOX to terminate a script’s execution thread early before it finishes.

Add this to the top or bottom of your script. It’ll delay the closing time by x seconds. Which should give enough time for studio to finish saving.

local RunService = game:GetService("RunService")
if RunService:IsStudio() then -- Only hook the function if you're in studio mode
	game:BindToClose(function()
		wait(3) -- Wait 3 seconds before shutting down the game
	end)
end

that doesnt fix the issue, it doesnt even save in an actual game so adding this just makes testing more difficult when closing it out

I say keep the code in, since it does make a difference when saving data in studio. I didn’t take a look at the code provided but it’s usually the culprit. Give me a second.

Get The Datastore Editor plugin, and try looking at ur userId and see if the values actually saved

if u want to get in a real game, leave the game, open studio Datastore plugin and check, with your userId and the key

I did, and found out how awful and limiting it was, so no Im not going to ds2

no, only one game, dungeon quest uses ds2 for saving as big games like jailbreak and any other front page game uses there own system. I also use my own data module on my other games and those save fine with less complexity then ds2. If you are just going to keep telling me to use datastore2 leave my post as I want to hear from people who have answers and not people who force datastore2 down my throat. thank you

bump

30 chaqrssadasd
asdfdsfdsfdsf

As it turns out the issue was on the WFC command in the data table. I misspelled a capital letter causes the code to yield leading to the data never saving and stopping the script. Now that I changed that it fixed the issue. I should have seen that :man_facepalming:. I will still use your bind to close just in case.

it has been solved

3- charasdasdasdasdsadsadsad