How do I access a specific players leaderstats from the server side?

Hi, i’m trying to access a specific players leaderstats so that I can change the value of one of the stats, but I keep getting the error message that ‘player2’ is not a part of Players. Any ideas why this script isn’t working?

local plr1Name = player1.Name
				local plr2Name = player2.Name
				
				game.Players.PlayerRemoving:Connect(function(player)
					
					if player.Name == plr1Name then
						local plr2Leaderstats = Players.player2:WaitForChild("leaderstats")
						plr2Leaderstats.Wins.Value += 1
					elseif player.Name == plr2Name then
						print("Player2 left")
					end
				end)

When using PlayerRemoving, it does changes to that specific player whose leaving


Im not really sure what this is.


But in this case you are trying to get their name,
Try this:

local Player1 = "Player1"
local Player2 = "Player2"

game.Players.PLayerRemoving:Connect(function(p)
if p.Name == Player1 then

local ls = p:WaitForChild("leaderstats")
ls.Wins.Value += 1
elseif p.Name == Player2 then
print(p.Name, "Has Left")
end


end)
1 Like

Thanks it’s working but the problem is that it’s adding the wins to player1, I need to so that whichever player leaves, it adds the win to the opposite player, so when player1 leaves, player2 gets + 1 win.

Create a table so when leaving, it removes the Player who left, but it gives the rest inside the Win

local Player1 = "Player1"
local Player2 = "Player2"

game.Players.PLayerRemoving:Connect(function(p)
if p.Name == Player1 then
table.remove(Player, p)

for _.Plr in pairs(Player) do
Plr:WaitForChild("leaderstats").Wins.Value += 1
end

elseif p.Name == Player2 then
print(p.Name, "Has Left")
end


end)
1 Like

Thank you very much, i’m really bad at using tables, so my script isn’t working. What is wrong with this?

                local Plr = {
					
				}
				
				table.insert(Plr, player1)
				table.insert(Plr, player2)
				
				local player1 = "Player1"
				local player2 = "Player2"
				
				game.Players.PlayerRemoving:Connect(function(p)
					if p.Name == player1 then
						table.remove(player1, p)

						for _.Plr in pairs(player2) do
							Plr:WaitForChild("leaderstats").Wins.Value += 1
						end

					elseif p.Name == player2 then
						print(p.Name, "Has Left")
					end


				end)

Try using a find first child. An object may not be found to the parent. So also use a pcall to state a success or error. And, Are u actually including real playernames?

1 Like

Hi, i’m going to be honest I don’t really understand what is going on myself. I have never scripted stuff like this