Hello, I have several script 2 in ServerServiceScript and one in the player all are server script, where the player's position is saved, that works perfectly, then I have the level and experience that work if the player dies or is reset, but when I leave the game either in stop or in the menu button leave, the experience and level data are erased, from the same roblox application, but if the position is saved perfectly. What can be?
--script Menu in ServerServiceScript
local Players = game:GetService("Players")
local MenuEventSave = game.ReplicatedStorage:WaitForChild('MenuEventSave')
local MenuEventReset = game.ReplicatedStorage:WaitForChild('MenuEventReset')
local DataStoreService = game:GetService("DataStoreService")
local DataPosition = DataStoreService:GetDataStore("PlayerPosition")
local DataLevel = DataStoreService:GetDataStore("PlayerLevel")
local DataExp = DataStoreService:GetDataStore("PlayerExp")
local lastPosition
local tableToVector = {43.809, 112.453, 103.157}
MenuEventSave.OnServerEvent:Connect(function()
for i, playerChild in pairs(Players:GetChildren()) do
for i, player in pairs(Players:GetPlayers()) do
lastPosition = player.Character.HumanoidRootPart.Position
if lastPosition then
DataPosition:SetAsync("Player_" .. player.UserId, {lastPosition.X,lastPosition.Y,lastPosition.Z})
DataLevel:SetAsync("Player_" .. player.UserId, player.leaderstats.Level.Value)
DataExp:SetAsync("Player_" .. player.UserId, player.leaderstats.EXP.Value)
print("Set1")
end
end
end
end)
MenuEventReset.OnServerEvent:Connect(function()
for i, playerChild in pairs(Players:GetChildren()) do
for i, player in pairs(Players:GetPlayers()) do
lastPosition = player.Character.HumanoidRootPart.Position
if lastPosition then
DataPosition:SetAsync("Player_" .. player.UserId, tableToVector)
DataLevel:SetAsync("Player_" .. player.UserId, 0)
DataExp:SetAsync("Player_" .. player.UserId, 0)
print("Set2")
end
end
end
end)
-- script load and died save ServerServiceScript
local DataStoreService = game:GetService("DataStoreService")
local DataPosition = DataStoreService:GetDataStore("PlayerPosition")
local DataLevel = DataStoreService:GetDataStore("PlayerLevel")
local DataExp = DataStoreService:GetDataStore("PlayerExp")
local players = game:GetService("Players")
local lastPosition
local debounce = false
players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder", player)
stats.Name = "leaderstats"
local level = Instance.new("IntValue", stats)
level.Name = "Level"
level.Value = 0
local exp = Instance.new("IntValue", stats)
exp.Name = "EXP"
exp.Value = 0
local position = DataPosition:GetAsync("Player_" .. player.UserId)
if position then
player.Character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(position[1],position[2],position[3])
end
DataLevel:GetAsync("Player_" .. player.UserId)
DataExp:GetAsync("Player_" .. player.UserId)
player.CharacterRemoving:Connect(function(character)
lastPosition = character.HumanoidRootPart.Position
if lastPosition then
DataPosition:SetAsync("Player_" .. player.UserId, {lastPosition.X,lastPosition.Y,lastPosition.Z})
print("Died")
end
local success, err = pcall(function()
DataLevel:SetAsync("Player_" .. player.UserId, player.leaderstats.Level.Value)
DataExp:SetAsync("Player_" .. player.UserId, player.leaderstats.EXP.Value)
end)
if success then
print("Success!")
end
position = DataPosition:GetAsync("Player_" .. player.UserId)
if position then
player.Character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(position[1],position[2],position[3])
end
DataLevel:GetAsync("Player_" .. player.UserId)
DataExp:GetAsync("Player_" .. player.UserId)
end)
end)
-- Script player level and experience ++
local Players = game:GetService("Players")
for i, player in pairs(Players:GetPlayers()) do
for i, enemy in pairs(game.Workspace:GetChildren()) do
while wait(0.01) do
if enemy:IsA('Model') and enemy.Name == "Enemy" and not Players:GetPlayerFromCharacter(enemy) then
local humanoidEnemy = enemy:FindFirstChild("Humanoid")
humanoidEnemy.Died:Connect(function()
player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
player.leaderstats.EXP.Value = player.leaderstats.EXP.Value + 5
print("Kill")
end)
end
break
end
end
end

5 comments
Even save the game from the save button does not save me when leaving nothing
You are using IntValues to store Level and EXP in contrast to Position where a tableToVector is used (if I'm not mistaken). Use the .Changed(function(updVal)) this method stores the "Updated" values for your EXP and Level on kills. It will also fix your DataStore issues, for DataLevel and DataExp as it was storing base values instead of updated ones.
Thamk you xxcoordinatorxx
But with what variable I use the function .Changed (function (updVal))
It goes at the bottom where you're updating leaderstats on kills.