Why is this table not working?

Hi, i’m trying to add 1 to the ‘win’ stat to a specific player when another player leaves. Basically there are two players in a 1v1 and if one of them leaves the game then the other player should have + 1 to their ‘win’ stat. I don’t really understand as my friend was helping me with it a while ago, but for some reason its not working and has got red lines under some parts of it. Any idea whats wrong?

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)

image

You have a period instead of a comma.

1 Like

The red underline is because You put a . Instead of a ,
It should be

for _,Plr in pairs(player2) do

your script seems to have other issues but before i judge anything i must ask a question

Will these variables always be a string of the players name?

local Plr = {}


local player1 = "Player1"
local player2 = "Player2"

table.insert(Plr, player1) -- you're trying to add these to a table before you've defined them, make sure they're below where you define them.
table.insert(Plr, player2)


game.Players.PlayerRemoving:Connect(function(p)
	if table.find(Plr, p.Name) then
		table.remove(Plr, table.find(Plr, p.Name)) -- might be better to showcase the usage of table.find and table.remove here. 
		
		for i, v in pairs(Plr) do -- might be a better idea to use Plr[1] here if theres only going to be 1 value left. As previously stated you've declared your loop incorrectly.
			if game.Players:FindFirstChild(v) then
				local Player = game.Players[v]
				Player:WaitForChild("leaderstats"):WaitForChild("Wins").Value += 1
			end
		end

	end
end)

2 Likes

These variables are previously assigned to the actual player, so i think ill change that. But to be honest I don’t really know what my friend was doing. If you have any suggestions I would love to hear them, I’ll take no offence. Thanks

I see, also i think the post above your reply (josh’s post) is the solution your looking for

1 Like

Thank you very much, I just tried out the script and it didn’t add a win to the player that didn’t leave the game for some reason. I didn’t have any error messages though. Any idea why?

My mistake. You’re storing a string yet table.find is searching for the instance of the player rather than the name. Change table.find(Plr, p) to table.find(Plr, p.Name) and it should remedy the issue. I’ve updated my code to reflect this issue!

1 Like

It works perfectly, thank you so much I really appreciate your help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.