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)
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.
How @thegreatdabb said, you might have to change the error variable and see if it works
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.
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
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)
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.
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.
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)
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.