DataStore Not Saving!

hello everyone, I have been trying to make a game in roblox for about 5 months, I have encountered a problem and I hope you can help me, I have 3 values ​​in the leaderstats called JumpPower, Rebirths and Diamonds, Diamonds value is saved by datastore while jumppower and rebirths values ​​are not. I really couldn’t understand why this is so and I couldn’t figure it out I hope you can help me thank you…

local DataStoreService = game:GetService("DataStoreService")
local CurrencyDataStore = DataStoreService:GetDataStore("Public")

local function SaveData(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	local JumpPower = leaderstats and leaderstats:FindFirstChild("JumpPower")
	local Diamonds = leaderstats and leaderstats:FindFirstChild("Diamonds")
	local Rebirths = leaderstats and leaderstats:FindFirstChild("Rebirths")

	if JumpPower and Diamonds and Rebirths then
		local data = {
			JumpPower = JumpPower.Value,
			Diamonds = Diamonds.Value,
			Rebirths = Rebirths.Value
		}

		local success, error1 = pcall(function()
			CurrencyDataStore:SetAsync(tostring(player.UserId), data)
		end)
		if not success then
			warn("Fail:", error1)
		end
	end

end


game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")

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

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

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

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

	local success, result = pcall(function()
		return CurrencyDataStore:GetAsync(tostring(player.UserId))
	end)
	if success and result then
		JumpPower.Value = result.JumpPower or humanoid.JumpHeight
		Diamonds.Value = result.Diamonds or 0
		Rebirths.Value = result.Rebirths or 0
	else
		JumpPower.Value = humanoid.JumpHeight
		Diamonds.Value = 0
		Rebirths.Value = 0
		
	end

	local function onJumpPowerChanged(newValue)
		JumpPower.Value = newValue
		humanoid.JumpPower = newValue
	end

	JumpPower.Changed:Connect(onJumpPowerChanged)

	player.CharacterRemoving:Connect(function()
		SaveData(player)
	end)
end)

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

API service was open, I always pay attention to this, the problem is not related to this. I try constantly and constantly, but I cannot understand and solve the problem. The diamonds value is saved while the others are not. There are two codes related to rebirths and jumppower, which I think are independent of this code, but could the problem be related to this? I can’t think of any other option. Here is the codes basicly:

local player = game.Players.LocalPlayer
local JumpPower = player:WaitForChild("leaderstats"):WaitForChild("JumpPower")
local rebirths = player:WaitForChild("leaderstats"):WaitForChild("Rebirths")

local currentRebirth = 0

script.Parent.MouseButton1Click:Connect(function()
	if JumpPower.Value >= 10 and currentRebirth == 0 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end

		currentRebirth = 1

	elseif JumpPower.Value >= 100 and currentRebirth == 1 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end

		currentRebirth = 2

	elseif JumpPower.Value >= 250 and currentRebirth == 2 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	

		currentRebirth = 3

	elseif JumpPower.Value >= 400 and currentRebirth == 3 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	

	elseif JumpPower.Value >= 1200 and currentRebirth == 4 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	
		
	elseif JumpPower.Value >= 2500 and currentRebirth == 5 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	
		
	elseif JumpPower.Value >= 7500 and currentRebirth == 6 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	
		
	elseif JumpPower.Value >= 20000 and currentRebirth == 7 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	
		
	elseif JumpPower.Value >= 50000 and currentRebirth == 8 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	
		
	elseif JumpPower.Value >= 125000 and currentRebirth == 9 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	
		
	elseif JumpPower.Value >= 350000 and currentRebirth == 10 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	
		
	elseif JumpPower.Value >= 800000 and currentRebirth == 11 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	
		
	elseif JumpPower.Value >= 1000000 and currentRebirth == 12 then
		rebirths.Value = rebirths.Value + 1
		JumpPower.Value = 0

		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.JumpPower = 0.2
		end	
		
	end
end)
...

here is the second script
```lua
local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()

local debounce = false

-- JumpPower değerindeki değişiklikleri takip eden fonksiyon

local function onClick()
	if debounce == false then
		debounce = true
		plr.leaderstats.JumpPower.Value += math.max(1, plr.leaderstats.Rebirths.Value * 10)

		script.Parent.Parent.Sound:Stop() 
		script.Parent.Parent.Sound:Play() 
		debounce = false
	end
end

local function onJumpPowerChanged(newValue)
	char.Humanoid.JumpPower = newValue
end

-- JumpPower değeri değiştiğinde onJumpPowerChanged fonksiyonunu çalıştıran fonksiyon
plr.leaderstats.JumpPower.Changed:Connect(onJumpPowerChanged)

script.Parent.Activated:Connect(function()
	onClick()
end)

mouse.Button1Down:Connect(function()
	onClick()
end)
...

This is happening because sometimes the servers shuts down before it gets a chance to actually save your data.

Ideally, you should keep track of what data is saving, then only shut down once all data is completely saved.

You can delay the shutdown of the server using game:BindToClose() like this:

-- Place this near the top
local ConcurrentSaving = 0

-- Inside your SaveData function, modify it to look like this
local function SaveData(player)
	ConcurrentSaving += 1

	-- ... your code here ...

	ConcurrentSaving -= 1
end

-- Delay the server from shutting down until all data is saved properly
-- The server will forcibly shut down however if all data isn't saved within 10s
-- Otherwise it will hang forever if the SaveData function crashes and you lose
-- track of how many saves are happening.
game:BindToClose(function()
	local MaxSaveAttempts = 10
	local CurrentSaveAttempt = 0
	
	while ConcurrentSaving > 0 or CurrentSaveAttempt > MaxSaveAttempts do
		CurrentSaveAttempt += 1
		task.wait(1)
	end
end)

Now, the server should wait until all data is saved before shutting down.

1 Like

no, that’s not because of the situation you said. if it was as you said, the value of diamonds would not be recorded either, but without exception, the value of diamonds is recorded, others are not recorded.

I didn’t see that, sorry. If you print data just before it saves, what does it say in the output?

1 Like

I don’t know if this is true or not but I also noticed on my game the datas aren’t saving. Can this be an issue related to Roblox?

1 Like

it’s ok dude, it doesn’t write anything about data in the output

Here is the only ways you can block datastore hope you can understand more it’s rare for roblox datastore bugs or due to code error.

Character count in string cannot exceed 65,536 characters.

The maximum page size for an OrderedDataStore is 100.

GetAsync() request has exceeded the maximum queue size and Roblox is unable to process the requests at the current throughput.

SetAsync() request has exceeded the maximum queue size and Roblox is unable to process the requests at the current throughput.

IncrementAsync() request has exceeded the maximum queue size and Roblox is unable to process the requests at the current throughput.

UpdateAsync() request has exceeded the maximum queue size and Roblox is unable to process the requests at the current throughput.

GetSortedAsync() request has exceeded the maximum queue size and Roblox is unable to process the requests at the current throughput.

RemoveAsync() request has exceeded the maximum queue size and Roblox is unable to process the requests at the current throughput.

Since the increased maximum object size is 4 MB

Component Maximum Number of Characters
Data Store Name 50
Key Name 50
Scope 50
Data (Key Value) 4,194,304 per key

You can also read more about it here : Data Stores | Documentation - Roblox Creator Hub

1 Like

I’m sure I’ve read a lot of threads on this subject, but I couldn’t find anyone who had the same problem as I was and whose problem was solved.

Just use datastore2 should be better i also used to be like u . Follow the steps