Hello, im making level system and i stuck with a problem that it just dont saving, im using profile store, and i really dont understand why some data are saving but level data is not saving: Code
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Server = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local ProfileStats = require(ServerStorage.Modules.ModuleScripts.Data.PlayerStats)
local DEFAULT_DATA = require(script.Player)
local ProfileStore = require(Server.Modules.ProfileStore.ProfileStore)
local DATA_STORE_KEY = "PLAYER_DATA"
if RunService:IsStudio() then
DATA_STORE_KEY = "PLAYER_STUDIO_TEST"
end
local PlayerStore = ProfileStore.New(DATA_STORE_KEY, DEFAULT_DATA.DEFAULT_PROFILE)
local Profiles: {[player]: typeof(PlayerStore:StartSessionAsync())} = {}
local Local = {}
local Shared = {}
function Local.OnSpawn()
for _, player in Players:GetPlayers() do
task.spawn(Local.LoadProfile, player)
end
Players.PlayerAdded:Connect(Local.LoadProfile)
Players.PlayerRemoving:Connect(Local.RemoveProfile)
end
function Local.LoadProfile(player : Player)
local profile = PlayerStore:StartSessionAsync(`{player.UserId}`, {
Cancel = function()
return player.Parent ~= Players
end,
})
if profile == nil then
return player:Kick("ProfileStore Error 1: No data profile found. Try rejoining the game")
end
profile:AddUserId(player.UserId)
profile:Reconcile()
profile.OnSessionEnd:Connect(function()
Profiles[player] = nil
player:Kick("Profile seasion ended. Try rejoining")
end)
local inGame = player.Parent == Players
if inGame then
Profiles[player] = profile
ProfileStats.New(player)
ProfileStats.SetStats(player, profile.Data.Level, profile.Data.Exp)
while true do
wait(1)
print(player.STATS.Level.Value)
end
else
profile:EndSession()
end
end
function Local.RemoveProfile(player : Player)
local profile = Profiles[player]
if profile ~= nil then
profile:EndSession()
end
end
function Shared.GetData(player : Player): DEFAULT_DATA.PlayerData?
local profile = Profiles[player]
if not profile then return end
return profile.Data
end
Local.OnSpawn()
return Shared
local PlayerStats = {}
local replicatedModules = game.ReplicatedStorage.Modules:WaitForChild("ModuleScripts")
local CalculateStats = require(replicatedModules:WaitForChild("CalculateStats"))
local remoteEvents = game.ReplicatedStorage.Remotes:WaitForChild("RemoteEvents")
local levelUpRE = remoteEvents:WaitForChild("OnLevelUp")
function PlayerStats.New(plr: Player)
local statsFolder = Instance.new("Folder")
statsFolder.Name = "STATS"
statsFolder.Parent = plr
local levelValue = Instance.new("IntValue")
levelValue.Name = "Level"
levelValue.Parent = statsFolder
local expValue = Instance.new("IntValue")
expValue.Name = "Exp"
expValue.Parent = statsFolder
end
function PlayerStats.AddExp(plr: Player, expToAdd: number)
local plrStats = plr:FindFirstChild("STATS")
if not plrStats then
warn(plr.Name .. " has no STATS folder! (AddExp)")
return
end
local plrLVL = plrStats:FindFirstChild("Level")
local plrEXP = plrStats:FindFirstChild("Exp")
if not plrLVL or not plrEXP then
warn(plr.Name .. " is missing Level or Exp Value! (AddExp)")
return
end
local currentLevel = plrLVL.Value
local newExp = plrEXP.Value + expToAdd
PlayerStats.SetStats(plr, nil, newExp)
local newLevel = plrLVL.Value
if newLevel > currentLevel then
levelUpRE:FireClient(plr)
end
end
function PlayerStats.SetStats(plr: Player, newLevel: number?, newExperience: number?)
local plrStats = plr:FindFirstChild("STATS")
if not plrStats then
warn(plr.Name .. " has no STATS folder! (SetStats)")
return
end
local plrLVL = plrStats:FindFirstChild("Level")
local plrEXP = plrStats:FindFirstChild("Exp")
if not plrLVL or not plrEXP then
warn(plr.Name .. " is missing Level or Exp Value! (SetStats)")
return
end
if not newLevel and not newExperience then
return
end
if not newLevel then
newLevel = CalculateStats.FromExp(newExperience)
end
if not newExperience then
newExperience = CalculateStats.FromLevel(newLevel)
end
plrLVL.Value = newLevel
plrEXP.Value = newExperience
end
return PlayerStats