Problem with saving Data

Script: Data

local DataStore = game:GetService("DataStoreService"):GetDataStore("Saves")
local saveRemote = game:GetService("ReplicatedStorage"):WaitForChild("Save")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
		local combatFolder = Instance.new("Folder", character)
		combatFolder.Name = "CombatFolder"
	end)
	
	local folder = Instance.new("Folder", player)
	folder.Name = "Data"
	
	local stamina = Instance.new("IntValue", folder)
	stamina.Name = "Stamina"
	
	local maxStamina = Instance.new("IntValue", folder)
	maxStamina.Name = "MaxStamina"
	
	local running = Instance.new("BoolValue", folder)
	running.Name = "Running"
	
	local inCombat = Instance.new("BoolValue", folder)
	inCombat.Name = "InCombat"
	
	local money = Instance.new("IntValue", folder)
	money.Name = "Money"
	
	local level = Instance.new("NumberValue", folder)
	level.Name = "Level"
	
	local exp = Instance.new("NumberValue", folder)
	exp.Name = "Exp"
	
	local requiredExp = Instance.new("NumberValue", folder)
	requiredExp.Name = "RequiredExp"
	
	local success, errormag = pcall(function()
		local getData = DataStore:GetAsync(player.UserId)
		for i, v in pairs(getData) do
			if i == 1 then
				stamina.Value = getData[i]
			elseif i == 2 then	
				maxStamina.Value = getData[i]
			elseif i == 3 then
				money.Value = getData[i]
			elseif i == 4 then
				level.Value = getData[i]
			elseif i == 5 then
				exp.Value = getData[i]
			elseif i == 6 then
				requiredExp.Value = getData[i]
			end						
		end
	end)
	
	if success then
		--print("Successfully loaded ".. player.Name.. "'s data!")
	elseif not success then
		-- FIRST TIME JOINING --
		stamina.Value = 100
		maxStamina.Value = 100
		money.Value = 0
		level.Value = 1
		exp.Value = 0
		requiredExp.Value = level.Value * 100
		--error("Something went wrong while loading the data of ".. player.Name.. ": ".. errormag)
	end
	
end)

saveRemote.OnServerEvent:Connect(function(player, val)
	DataStore:SetAsync(player.UserId, val)
	--print("Successfully saved ".. player.Name.. "'s data!")
end)

Local Script: AutoSaving

local remote = game:GetService("ReplicatedStorage"):WaitForChild("Save")
local data = game.Players.LocalPlayer:WaitForChild("Data")
local enabled = false

while true do
	wait(5) -- TIME FOR SAVE
	local saveDataTable = {}
	table.insert(saveDataTable, 1, data.Stamina.Value)
	table.insert(saveDataTable, 2, data.MaxStamina.Value)
	table.insert(saveDataTable, 3, data.Money.Value)
	table.insert(saveDataTable, 4, data.Level.Value)
	table.insert(saveDataTable, 5, data.Exp.Value)
	table.insert(saveDataTable, 6, data.RequiredExp.Value)
	remote:FireServer(saveDataTable)
	print("Autosaved ".. game.Players.LocalPlayer.Name.. "'s Data")
end 

So the preservation itself works, but for example levels are saved, but for example exp, or requiredXp is not saved. Here is an example, came in the first time got xp:

But when I restarted and didn’t do anything else:

1 Like

Please don’t use a remote event to tell the server to save data. Just use a loop on the server, which waits 60 seconds per save. The loop would call a function to save, so you can use this function in other places, such as when the player leaves the game.

ok I will change it, but still why exactly exp, and requiredXp are not saved? but converted to levels

There’s a lot to be improved in your script. But, why not use a module like Datastore2 or ProfileService for saving data? I’d say it’s much easier than trying to make your own system.

to be honest you have a bit safe script, but poorly optimized, first of all as @bluebxrrybot said it’s better to use dataStore2 How To Use Datastore2 - Advanced and Secure Datastore (Roblox Studio) - YouTube but remember that DataStore2 is recommended is for values
edit: instead of doing so many if conditionals at the time of obtaining the data it is better to obtain the direct data with a key