For some reason datastore saves only one data

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    For some reason datastore saves only old data not new one
  2. What is the issue? Include screenshots / videos if possible!
    image - first data
    image - data which should save
    image - new data
  3. 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) 
2 Likes

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

2 Likes

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)

1 Like

I know what i can EDIT ONLY BY SERVER. Every data is UPDATED BY SERVER not BY CLIENT

Okay maybe that studio glitch, because when i reopen studio new data saves

This script is working fine for me, I know because I wrote most of it.

image

Studio glitch as i understand, because when i re-open studio everything fine

Lemme try check it in game not in studio

Okay nvm even in game nothing saves

Idk it like have a biiiig cooldown, because yesterday i had other data today a new one, but i can’t save data again in same time

image
Wait i that a warn not a success that means data didn’t save…

IDK what’s the reason of cooldown, but when i saved first data i got error “too much datastore request. Other request will be deleted”

Yeah seems datastore get very much requests and this is a reason why data don’t save

You definitely can’t save 4 values in one datastore, here’s the format:

Key:
Value:


Of course, you could use a table, such as this:

local dataToSave = {
    player.leaderstats.Money.Value;
    player.leaderstats.Clicks.Value;
    player.leaderstats.Rebirths.Value
}

dataStore:SetAsync("Data_"..player.UserId, dataToSave)

That would generally save all the information. I hope this helped a little! :smiley:

It’s already being saved as a table, his issue is an infinite yield on an instance which doesn’t exist.

I already fixed that this didn’t help data still work strange

I do not know what to do everything fine, but data still saves strange

image
Here is a error i think this is a problem of datastore

This is because you’re entering/exiting the game too frequently.