I failed in datastore Please Help me

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, they are displayed properly in the game, but I want these data to be recorded, but unfortunately i couldn’t do it can you help me this is the code i wrote

local DataStoreService = game:GetService("DataStoreService")
local JumpPowerDataStore = DataStoreService:GetDataStore("JumpPowerDataStore")
local DiamondsDataStore = DataStoreService:GetDataStore("DiamondsDataStore")
local RebirthsDataStore = DataStoreService:GetDataStore("RebirthsDataStore")

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

	if JumpPower and Rebirths and Diamonds then
		
		local success, error = pcall(function()
			JumpPowerDataStore:SetAsync(tostring(player.UserId), JumpPower.Value)
		end)
		if not success then
			warn("JumpPowerDataStore kaydetme hatası:", error)
		end

		
		local success, error = pcall(function()
			RebirthsDataStore:SetAsync(tostring(player.UserId), Rebirths.Value)
		end)
		if not success then
			warn("RebirthsDataStore kaydetme hatası:", error)
		end

		
		local success, error = pcall(function()
			DiamondsDataStore:SetAsync(tostring(player.UserId), Diamonds.Value)
		end)
		if not success then
			warn("DiamondsDataStore kaydetme hatası:", error)
		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 success, result = pcall(function()
		return JumpPowerDataStore:GetAsync(tostring(player.UserId))
	end)
	if success and result then
		JumpPower.Value = result
	else
		JumpPower.Value = humanoid.JumpHeight
	end

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

	
	local success, result = pcall(function()
		return RebirthsDataStore:GetAsync(tostring(player.UserId))
	end)
	if success and result then
		Rebirths.Value = result
	else
		Rebirths.Value = 0
	end

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

	
	local success, result = pcall(function()
		return DiamondsDataStore:GetAsync(tostring(player.UserId))
	end)
	if success and result then
		Diamonds.Value = result
	else
		Diamonds.Value = 0
	end

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

	
	JumpPower.Changed:Connect(onJumpPowerChanged)

	
	local function onStatsChanged()
		SaveData(player)
	end

	
	Rebirths.Changed:Connect(onStatsChanged)
	Diamonds.Changed:Connect(onStatsChanged)
end)

“error” is a lua global, so you can’t use it as a variable. Try using “fail” instead

1 Like

Actually ı stuck this problem but ı wıll change the error message thanks sir

1 Like

Hya, first of all, you are saving the data everytime you are changing a currency value, which is not optimal, you should add a task.spawn or coroutine.wrap that saves data every 1/2 minutes.

  1. How @thegreatdabb said, you might have to change the error variable and see if it works

  2. Try to use a single DataStore, it’s not optimal making 3 saving ONLY 1 value.

Example on Saving data: DataStore:SetAsync(Player.UserId, {
[“JumpPower”] = Player.Character.Humanoid.JumpPower,
[“Diamonds”] = Player.Diamonds.Value
}) — Again you would need some pcalls to check more stuff before saving the data

Example of requesting data:
Player.Diamonds.Value = DataStoreSuccess.Diamonds or 0 — If the player is new it will automatically load with the default value.

1 Like

I changed the code taking into account what you said, but there is a problem, rebirths and jumppower values ​​are not saved while saving the diamonds value, this is the final code.

local DataStoreService = game:GetService(“DataStoreService”)
local CurrencyDataStore = DataStoreService:GetDataStore(“CurrencyDataStore”)

local function SaveData(player)
local leaderstats = player:WaitForChild(“leaderstats”)
local JumpPower = leaderstats:FindFirstChild(“JumpPower”)
local Diamonds = leaderstats:FindFirstChild(“Diamonds”)
local Rebirths = leaderstats:FindFirstChild(“Rebirths”)

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

	local success, error = pcall(function()
		CurrencyDataStore:SetAsync(tostring(player.UserId), data)
	end)
	if not success then
		warn("Fail:", error)
	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)

local function onStatsChanged()
	SaveData(player)
end

Diamonds.Changed:Connect(onStatsChanged)
Rebirths.Changed:Connect(onStatsChanged)


while true do
	wait(30) 
	SaveData(player)
end

end)

Here is the code modified:


local DataStoreService = game:GetService(“DataStoreService”)
local CurrencyDataStore = DataStoreService:GetDataStore(“CurrencyDataStore”)

local function SaveData(player)
local leaderstats = player:WaitForChild(“leaderstats”)
local JumpPower = leaderstats:FindFirstChild(“JumpPower”)
local Diamonds = leaderstats:FindFirstChild(“Diamonds”)
local Rebirths = 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)

while task.wait(60) do SaveData(player) end

end) 

game.Players.PlayerRemoved:Connect(SaveData)

1 Like

I haven’t changed almost nothing as I don’t have the ability to go on pc, can you please provide me more informations like if there are any errors? Press F9 or enable output log on studio to see any bugs.

1 Like

hello sir, I copied the code you posted and opened the output. I couldn’t see any errors related to this issue in the output, it doesn’t save any value at the moment.

Do you have api services enabled?

1 Like

Yes sir, i am enabled Ağı services

I’ll try to fix ur code after I get home so I can see what’a the issue. I might think it’s from returning the GetAsync.

1 Like

I am very grateful to you my friend, thank you very much, I am waiting for your return

I tried to run the code and everything works fine, are you sure you have API Services enabled?

try this:

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

local function SaveData(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local JumpPower = leaderstats:FindFirstChild("JumpPower")
	local Diamonds = leaderstats:FindFirstChild("Diamonds")
	local Rebirths = 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)

	while task.wait(60) do SaveData(player) end

end) 

game.Players.PlayerRemoving:Connect(SaveData)

As u can see sir my diamonds value is saved from datastore but my jumppower and rebirths value is not saving. Your post deleted sir ı don’t understand

I am afraid you give stats from client and not from server.

1 Like

now i created a server and performed the test on the server, first of all thank you for helping me and sorry for taking your time. I went in and out of the server and saw that the jumppower and rebirths values ​​were not saved while the diamonds value was saved, sir. I really don’t understand why one value saved while the others don’t.

It’s okay, if this fixed your problem you can mark it as solution.

1 Like

I can’t say my problem is solved sir because only 1 of the 3 values ​​is saved so my problem is still solved I’m very sorry