Hello! Im new to data stores and Im trying to save 3 leader board values, Coins, Presses, and Minutes
the problom is, the script only saves coins.
I have it print after it saves each one and it does so, but still, only the coins save.
I have no idea whats wrong. heres the script.
– At the time of posting this Im at school so my responds will be slow –
local datastore = game:GetService("DataStoreService")
local Store1 = datastore:GetDataStore("Coins")
local Store2 = datastore:GetDataStore("Presses")
local Store3 = datastore:GetDataStore("Minutes")
game.Players.PlayerAdded:connect(function(plr)
local LeaderStatsFolder = Instance.new("Folder", plr)
local Store1Value = Instance.new("IntValue", LeaderStatsFolder)
local Store2Value = Instance.new("IntValue", LeaderStatsFolder)
local Store3Value = Instance.new("IntValue", LeaderStatsFolder)
LeaderStatsFolder.Name = "leaderstats"
-------------------------------------------------------------------------------
Store1Value.Name = "Coins"
Store2Value.Name = "Presses"
Store3Value.Name = "Minutes"
local DebugPrint = game.ReplicatedStorage.DebugStuff.PrintDebugStuff.Value
-------------------------------------------------------------------------------
Store1Value.Value = Store1:GetAsync(plr.UserId) or 0
Store1:SetAsync(plr.UserId, Store1Value.Value)
Store2Value.Value = Store2:GetAsync(plr.UserId) or 0
Store2:SetAsync(plr.UserId, Store2Value.Value)
Store2Value.Value = Store3:GetAsync(plr.UserId) or 0
Store3:SetAsync(plr.UserId, Store3Value.Value)
-------------------------------------------------------------------------------
while true do
wait(33)
Store1:SetAsync(plr.UserId, Store1Value.Value)
if DebugPrint == 1 then
print("Saved Coins")
end
wait(33)
Store2:SetAsync(plr.UserId, Store2Value.Value)
if DebugPrint == 1 then
print("Saved Button Presses")
end
wait(33)
Store3:SetAsync(plr.UserId, Store3Value.Value)
if DebugPrint == 1 then
print("Saved Total Time Played")
end
end
end)
In your output log do you receive anything about datastore requests being queued?
Also it would be better to just store all the values in a table so then you wouldn’t be using all of your data requests per minute so quickly
Have you printed what the datastore has stored to know that it is actually saving? Also do you have API services enable so that you can use Datastore Service?
After it saved I printed the saved value and for coins it printed the amount but for minutes and button presses it just printed 0 no matter what there values are
local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")
local PlayerDataStore = DatastoreService:GetDataStore("PlayerData")
local PlayerDataTable = {
Coins = 0,
Presses = 0,
Minutes = 0,
}
function GetData(Player)
if not Player return end -- if player is not in game end function
local Data
local Success, Response = pcall(function()
Data = PlayerDataStore:GetAsync(Player.UserId)
end)
if Success and not Data then --// If Data Retrival Sucessful and Data == nil
warn(Player.Name .. " has no Data ... Setting Up Datastore")
local DataTable = PlayerDataTable
return DataTable
else --// If Data Retrival Sucessful and Data ~= nil
warn(Player.Name .. " hasData ... Data Retrieval Successful")
return Data
end
end
function UpdateData(Player)
if not Player return end -- if player is not in game end function
local LeaderStatsFolder = Player.leaderstats
local Coins = LeaderStatsFolder.Coins
local Presses = LeaderStatsFolder.Presses
local Minutes = LeaderStatsFolder.Minutes
local DataTable = PlayerDataTable
local Success, Response = pcall(function()
PlayerDataStore:UpdateAsync(Player.UserId, function(OldTable)
local NewTable = DateTime --// Unintentional error / forgot to make update table
return NewTable
end)
end)
end
Players.PlayerAdded:connect(function(Player)
--[[ local LeaderStatsFolder = Instance.new("Folder", plr)
First it isnt recommended to use the Second Param of Instance.new
You want to set the instances properties before its Parent
]]
local LeaderStatsFolder = Instance.new("Folder")
LeaderStatsFolder.Name = "leaderstats"
LeaderStatsFolder.Parent = Player
local Store1Value = Instance.new("IntValue")
Store1Value.Name = "Coins"
Store1Value.Parent = LeaderStatsFolder
local Store2Value = Instance.new("IntValue")
Store2Value.Name = "Presses"
Store2Value.Parent = LeaderStatsFolder
local Store3Value = Instance.new("IntValue")
Store3Value.Name = "Minutes"
Store3Value.Parent = LeaderStatsFolder
-------------------------------------------------------------------------------
local DebugPrint = game.ReplicatedStorage.DebugStuff.PrintDebugStuff.Value
-------------------------------------------------------------------------------
local Data = GetData(Player)
Store1Value.Value = Data.Coins
Store2Value.Value = Data.Presses
Store3Value.Value = Data.Minutes
-------------------------------------------------------------------------------
while true do
wait(30) -- // If wait is anyless than 30 seconds it will be overkill
UpdateData(Player)
end
end)
Error in the UpdateData function (i didn't even make the updatedata table at all)
function UpdateData(Player)
if not Player return end -- if player is not in game end function
local LeaderStatsFolder = Player.leaderstats
local Coins = LeaderStatsFolder.Coins
local Presses = LeaderStatsFolder.Presses
local Minutes = LeaderStatsFolder.Minutes
local Success, Response = pcall(function()
PlayerDataStore:UpdateAsync(Player.UserId, function(OldTable)
local NewTable = {Coins = Coins.Value, Presses = Presses.Value,Minutes = Minutes.Value } -- I didn't even make the update Datatable mybad
return NewTable
end)
end)
end
Updating Data when PlayerRemoving and ServerShutting Down
Players.PlayerRemoving:Connect(function(Player)
UpdateData(Player) -- Will Save the Data of the player leaving
end)
game:BindToClose(function()--//this function for saving data when game is shuting down
local players = Players:GetPlayers()
for _, Player in pairs(players) do
UpdateData(Player)
end
end)
I was rushing and forgot to make the update data table mybad.
Hello again! I’m back from school and was testing the script and realized, it doesn’t seem to save. I’ve looked through it and have no clue why it’s not saving. I’ve added a print for every time it saves and yet, I stop and play again and it just reset back to 0 and sets up the datastore again. Could you help me with this? I’m clueless at this point.