Datastore Not Working ..Again

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 :frowning:

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)


You are changing it on the client

Never trust the client to save any kind of values, they can change it to whatever they want

ONLY save data on the server


P.s. please format your code properly it’s very difficult to read it like that

1 Like

Oh. Well let me go change that up and see if it works!

Omg it worked! Thanks so much!

1 Like