Datastore is not saving my data!

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)

In a testing game that I have, I inserted your script and checked for anything that might go wrong.

The first thing I noticed is that you directly reference Players via game.Players. What happens if Players is renamed (for whatever reason)? The script breaks. I swapped that for game:GetService("Players"). This makes sure that you always fetch the service and not anything else that’s named Players. I edited values on the server and noticed that everything saved on reset. I even stopped the test and redid it again. All of the values stayed.

Try the game:GetService trick and let me know how that fairs

Edit: See TaxFraudBruh’s answer (Datastore is not saving my data! - #3 by TaxFraudBruh). API Services might be the issue

1 Like

Do you have API Services enabled?
I believe it is the reason why it won’t save, I ran the test and it was printing the error 404 when the game is not published to Roblox. But if the game is published to Roblox, the error is 403: Cannot write to DataStore from studio if API access is not enabled. There’s your solution!


1 Like

hi firstly thanks your advice sir… 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)

second script:

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)

When you are testing, I recommend using print() and showing the values your server is going to send. Once it’s sent, then you should also insert a print statement where the code fetches the values. This way, you can test for 2 cases

  1. The server is sending bad data due to your code. This could cause the save to fail entirely
  2. DataStore is retrieving outdated/bad data. This would be a Roblox issue

Linking duplicate post: DataStroe not saving some values