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: