So the issue is, the data saves when I change it in the Console, but when a script changes it, like game.Players.LocalPlayer.leaderstats.Level.Value = game.Players.LocalPlayer.leaderstats.Level.Value + 5 it NEVER saves when the player leaves ![]()
local DataStoreService = game:GetService(“DataStoreService”)
local playerData = DataStoreService:GetDataStore(“PlayerData”)
local function onPlayerJoin(player) – Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Level = Instance.new("IntValue") --Sets up value for leaderstats
Level.Name = "Level"
Level.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
-- Data exists for this player
Level.Value = data
else
-- Data store is working, but no current data for this player
Level.Value = 0
end
end
local function onPlayerExit(player) --Runs when players exit
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player.leaderstats.Level.Value) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)