You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
For some reason datastore saves only old data not new one
What is the issue? Include screenshots / videos if possible!
- first data
- data which should save
- new data
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
My code:
local playersService = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local dataStore = dataStores:GetDataStore("DataStore")
local protectedCall = pcall
local function onPlayerJoined(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local clicks = Instance.new("IntValue")
clicks.Name = "Clicks"
clicks.Parent = leaderstats
local money = Instance.new("NumberValue")
money.Name = "Money"
money.Parent = leaderstats
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
local PlayerGUI = player:WaitForChild("PlayerGui")
local StatsFolder = PlayerGUI:WaitForChild("Stats")
local RCAmount = StatsFolder:WaitForChild("RCAmount")
local success, result = protectedCall(function()
return dataStore:GetAsync("Data_"..player.UserId)
end)
if success then
if result then
if type(result) == "table" then
clicks.Value = result[1]
money.Value = result[2]
rebirths.Value = result[3]
RCAmount.Value = result[4]
end
end
else
warn(result)
end
end
local function onPlayerLeft(player)
local success, result = protectedCall(function()
return dataStore:SetAsync("Data_"..player.UserId, {player.leaderstats.Clicks.Value, player.leaderstats.Money.Value, player.leaderstats.Rebirths.Value, player.PlayerGui.Stats.RCAmount.Value})
end)
if success then
print(result)
else
warn(result)
end
end
local function onServerClosed()
for _, player in ipairs(playersService:GetPlayers()) do
local success, result = protectedCall(function()
return dataStore:SetAsync("Data_"..player.UserId, {player.leaderstats.Clicks.Value, player.leaderstats.Money.Value, player.leaderstats.Rebirths.Value,player.PlayerGui.Stats.RCAmount.Value})
end)
if success then
print(result)
else
warn(result)
end
end
end
playersService.PlayerAdded:Connect(onPlayerJoined)
playersService.PlayerRemoving:Connect(onPlayerLeft)
game:BindToClose(onServerClosed)
if you read your code before asking for help you can see that it’s only saving one datatype “onServerClosed” function
local success, result = protectedCall(function()
return dataStore:SetAsync("Data_"..player.UserId, {player.leaderstats.Clicks.Value, player.leaderstats.Money.Value, player.leaderstats.Rebirths.Value,player.PlayerGui.Stats.RCAmount.Value})
end)
I recommend getting an actual understanding of how to interact with rlua tables and how you would go about GetAsync() and SetAsync()'ing. alternatively you can just use the several various resources and tutorials such as Datastore2 and Profileservice
Changes to the values assigned to the “Value” properties shared by “ValueBase” instances (the base class for NumberValue, IntValue, StringValue, BoolValue instances etc.) performed on the client will not replicate to the server. You have been told this several times.
-- // Configurations
local DataName = "BLABLA" -- Edit this
local FolderName = "leaderstats" -- Edit this
local FolderType = "Folder" -- Edit this
local StatsName1 = "Name1" -- Edit this
local StatsValue1 = 0 -- Edit this
local StatsType1 = "NumberValue" -- Edit this
local StatsName2 = "Name2" -- Edit this
local StatsValue2 = 0 -- Edit this
local StatsType2 = "NumberValue" -- Edit This
-- // End of configurations
local DataStoreService = game:GetService("DataStoreService")
local statsStore = DataStoreService:GetDataStore(DataName)
local PlayersService = game:GetService("Players")
function saveStats(PlayerFunction)
local leaderFolder = Instance.new(FolderType, PlayerFunction)
leaderFolder.Name = FolderName
local statsValue1 = Instance.new(StatsType1, leaderFolder) -- Use this "NumberValue" , leaderFolder instead of .Parent, to make the code more stable
statsValue1.Name = StatsName1
local statsValue2 = Instance.new(StatsType2, leaderFolder)
statsValue2.Name = StatsName2
local prlKey= "Player_" .. PlayerFunction.UserId
local oldData
local foundData,newplr = pcall(function()
oldData = statsStore:GetAsync(prlKey)
end)
if foundData and oldData ~= nil then
statsValue1.Value = foundData[1]
statsValue2.Value = foundData[2]
print("Data loaded for: " ..PlayerFunction.Name)
else
statsValue1.Value = StatsValue1
statsValue2.Value = StatsValue2
print("New data for: " ..PlayerFunction.Name)
end
end
PlayersService.PlayerAdded:Connect(function(PlayerAdded)
local succesLoaded,ErrorLoaded = pcall(function()
saveStats(PlayerAdded)
end)
if succesLoaded then
print("Data loaded for: " ..PlayerAdded.Name)
else
print("Data not loaded: " ..PlayerAdded.Name)
end
end)
PlayersService.PlayerRemoving:Connect(function(PlayerRemoved)
local leaderstats = PlayerRemoved:FindFirstChild(l"FolderName") -- EditThis
local prlKey= "Player_" .. PlayerRemoved.UserId
local valueToSave = {
leaderstats:FindFirstChild("StatsName1"); -- Edit this
leaderstats:FindFirstChild("StatsName2"); -- Edit this
}
local succesSaved,ErrorSaved = pcall(function()
statsStore:SetAsync(prlKey,valueToSave)
end)
if succesSaved then
print("Data saved! for: "..PlayerRemoved.Name)
else
print("Data not saved! for: "..PlayerRemoved.Name)
end
end)
game:BindToClose(function()
for _, allplayers in pairs(PlayersService:GetChildren()) do
local succesSaved,ErrorSaved = pcall(function()
saveStats(allplayers)
end)
if succesSaved then
print("Data saved for:".. allplayers.Name)
else
print("Data not saved for:".. allplayers.Name)
end
end
end)