After leaderstats have been changed the values won't save

as i said after leaderstats have been changeed the values won’t save.
here is the script:

local DataStore = DataStoreService:GetDataStore("LeaderstatsDataStoreTest222")

game.Players.PlayerAdded:Connect(function(player: Player)	
	-- Create the leaderstats
	local leaderStats = Instance.new("Folder")
	leaderStats.Name = "leaderstats"
	leaderStats.Parent = player

	-- Create the Coins value
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 10
	coins.Parent = leaderStats

	-- Create the Gems value
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Value = 0
	gems.Parent = leaderStats

	-- Update this player's stats from DataStore
	local success, data = pcall(DataStore.GetAsync, DataStore, player.UserId)

	if success then
		coins.Value = data.Coins
		gems.Value = data.Gems
	end	
	
end)

for i, player in pairs(game.Players:GetPlayers()) do
	while task.wait(10) do
		local data = {
			Gems = player.leaderstats.Gems.Value,
			Coins = player.leaderstats.Coins.Value
		}

		local success, errormsg = pcall(DataStore.SetAsync, DataStore, player.UserId, data)
		if success then
			print("Data Saved1")
		else
			warn(errormsg)
		end
	end
end
game.Players.PlayerRemoving:Connect(function(player)
	local data = {
		Gems = player.leaderstats.Gems.Value,
		Coins = player.leaderstats.Coins.Value
	}

	local success, error = pcall(DataStore.SetAsync, DataStore, player.UserId, data)
end)
1 Like

Do you get any error messages?

Does your “Data saved” print function run?

What do you see if you check the data right before they get saved when the player leaves? (try printing it)

please put three backticks for the whole code, not a part

```lua
put code here
```

i dont get any errors so im very confused

local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“LeaderstatsDataStoreTest222”)

game.Players.PlayerAdded:Connect(function(player: Player)
– Create the leaderstats
local leaderStats = Instance.new(“Folder”)
leaderStats.Name = “leaderstats”
leaderStats.Parent = player

-- Create the Coins value
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 10
coins.Parent = leaderStats

-- Create the Gems value
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Value = 0
gems.Parent = leaderStats

-- Update this player's stats from DataStore
local success, data = pcall(DataStore.GetAsync, DataStore, player.UserId)

if success then
	coins.Value = data.Coins
	gems.Value = data.Gems
end	
end)

for i, player in pairs(game.Players:GetPlayers()) do
while task.wait(10) do
local data = {
Gems = player.leaderstats.Gems.Value,
Coins = player.leaderstats.Coins.Value
}

	local success, errormsg = pcall(DataStore.SetAsync, DataStore, player.UserId, data)
	if success then
		print("Data Saved1")
	else
		warn(errormsg)
	end
end
end
game.Players.PlayerRemoving:Connect(function(player)
local data = {
Gems = player.leaderstats.Gems.Value,
Coins = player.leaderstats.Coins.Value
}

local success, error = pcall(DataStore.SetAsync, DataStore, player.UserId, data)
end)

fixed ya format