Studio doesnt save leaderboard values on one game, but it does on the other

Alright, so Im trying to make a leaderboard script that saves the values from one place to another. However, it doesn’t work. For some reason though, the same script works perfectly fine on another game.
I have Access to studio API services turned on on both games.

Here is my script:

local DataStoreService = game:GetService(“DataStoreService”)

local DataStore = DataStoreService:GetDataStore(“MoneyStats”)

game.Players.PlayerAdded:Connect(function(Player)

local Leaderstats = Instance.new(“Folder”, Player)

Leaderstats.Name = “leaderstats”

local Currency = Instance.new(“IntValue”, Leaderstats)

Currency.Name = “PlaceID”

Currency.Value = 0

local Data = DataStore:GetAsync(Player.UserId)

if Data then

Currency.Value = Data
end

end)

game.Players.PlayerRemoving:Connect(function(Player)

DataStore:SetAsync(Player.UserId, Player.leaderstats.PlaceID.Value)
end)

local DataStoreService = game:GetService(“DataStoreService”)

local DataStore = DataStoreService:GetDataStore(“PlaceIDStats”)

game.Players.PlayerAdded:Connect(function(Player)

local Leaderstats = Instance.new(“Folder”, Player)

Leaderstats.Name = “leaderstats”

local PlaceID= Instance.new(“IntValue”, Leaderstats)

PlaceID.Name = “PlaceID”

PlaceID.Value = 0

local PreviousePlaceID= Instance.new(“IntValue”, Leaderstats)

PreviousePlaceID.Name = “PreviousePlaceID”

PreviousePlaceID.Value = 0

local Data = DataStore:GetAsync(Player.UserId)

if Data then

PlaceID.Value = Data.PlaceID

PreviousePlaceID.Value = Data.PreviousePlaceID

end

end)

game.Players.PlayerRemoving:Connect(function(Player)

DataStore:SetAsync(Player.UserId, {

[“PlaceID”] = Player.leaderstats.PlaceID.Value;

[“PreviousePlaceID”] = Player.leaderstats.PreviousePlaceID.Value;

})

end)

Anyone able to help me fix this?

Sometimes when the player is the last one remaining, the server closes after they leave so you need to kick them before the server shuts down to do so you will add to your script

game:BindToClose(function()
for i,v in pairs(game.Players:GetPlayers()) do
v:Kick("Server Shutdown")
end
end)

this kicks the players before the server shuts down and since you have a PlayerRemoving connected your data will be saved. And also if you are testing it in studio I would recommend doing a TeamTest server than a local server or simply pressing F5

Hey! :slight_smile:
I fixed your script a little, I hope it helps you solve the error

local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("IntValue", Player)
	Leaderstats.Name = "leaderstats"

	local Place = Instance.new("IntValue", Leaderstats)
	Place.Name = "PlaceID"
	Place.Value = 0
	
	local PreviousePlaceID = Instance.new("IntValue", Leaderstats)
	PreviousePlaceID.Name = "PreviousePlaceID"
	PreviousePlaceID.Value = 0
	

	
	
	local PlayerID = "Player_"..Player.UserId
	
	
	local Data 
		
	local succes, errormessage = pcall(function()
		Data = DataStore1:GetAsync(PlayerID)
	end)
		
		
		
	if succes then
		
		if Data then
			Place.Value = Data.Place
			PreviousePlaceID.Value = Data.PreviousePlaceID
			print(PreviousePlaceID.Value)
		end
		
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local PlayerID = "Player_"..Player.UserId
	
	
	local DataTable = {
		PlaceID = Player.leaderstats.PlaceID.Value;
		PreviousePlaceID = Player.leaderstats.PreviousePlaceID.Value;
	}
	
	local succes, errormessage = pcall(function()
		DataStore1:SetAsync(PlayerID, DataTable)
		
		
	end)
	
	if succes then
		print("Succesfully Saved Data "..PlayerID)
		print(DataTable)
		
	else
		warn(errormessage)
	end
end)