Why is the player getting +3 stat rather than +1?

Hey, I have some script that runs when a player leaves the game, and it rewards the other player that didn’t leave with plus 1 ‘wins’. It was working perfectly fine, but all of a sudden its adding 3 wins rather than 1 when the other player wins. Any idea why this is happening?

					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

can’t see any problems, check Output for any errors

1 Like

Can you show more of the script? Like how are you adding players to Plr? How is this script running? Are there any other scripts?

1 Like

no errors, do you want to see more of the script because its ever since ive added other script

local function startEvent()

	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2
	
	for index,player in pairs(Players:GetPlayers()) do --Get all the players in the game
		--player is the player we are currently at
		--index is not used and mostly used for tables (don't get confused here, we don't use index in this script)
		local possibleTool = player1.Backpack:FindFirstChild("NoobSign")
		local possibleTool2 = player1.Character:FindFirstChild("NoobSign")
		local possibleTool3 = player2.Backpack:FindFirstChild("NoobSign")
		local possibleTool4 = player2.Character:FindFirstChild("NoobSign")
		
		if possibleTool then --if the tool is in the backpack
			possibleTool:Destroy()
		elseif possibleTool2 then --if it's equipped
			possibleTool2:Destroy()
		end
		
		if possibleTool3 then --if the tool is in the backpack
			possibleTool3:Destroy()
		elseif possibleTool4 then --if it's equipped
			possibleTool4:Destroy()
		end

	end
	
	local char1 = player1.Character
	local char2 = player2.Character

	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.colon.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.timer.Visible = false
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.colon.Visible = true
	game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.timer.Visible = false

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()

	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn

	print("Variables working fine")


	if char1 and char2 then


		local humanoid1 = player1.Character:FindFirstChild("Humanoid")
		local humanoid2 = player2.Character:FindFirstChild("Humanoid")


		if humanoid1 and humanoid2 then

			Sword1.Parent = player1.Backpack
			humanoid1:EquipTool(Sword1)
			Sword2.Parent = player2.Backpack
			humanoid2:EquipTool(Sword2)

			print("Approved that i'm humanoid")

			local connections = {}

			local function disconnectConnections(kills)
				if kills == 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end

					if plr1CurrentKills == 5 then
						winner = player1
					elseif plr2CurrentKills == 5 then
						winner = player2			
					end			

					player1.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					player2.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1

					if winner == player1 then
						Sword1:Destroy()
					elseif winner == player2 then
						Sword2:Destroy()
					end
					
					winner.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocs.SpawnLocation1.CFrame
					

					plr1CurrentKills = 0
					plr2CurrentKills = 0

					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Visible = false
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Visible = false
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.colon.Visible = false
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.timer.Visible = true
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Visible = false
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Visible = false
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.colon.Visible = false
					game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.timer.Visible = true


					part1.Transparency = 0
					part1.CanCollide = true
					part2.Transparency = 0
					part2.CanCollide = true
					return true
				end
				
				local Plr = {}


				local player1String = "Player1"
				local player2String = "Player2"

				table.insert(Plr, player1String) -- 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, player2String)


				game.Players.PlayerRemoving:Connect(function(p)
					
					if p == player1 then
						
						for i, v in ipairs(connections) do
							v:Disconnect()
						end
						
						player2.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
						
						for index,player in pairs(Players:GetPlayers()) do --Get all the players in the game
							--player is the player we are currently at
							--index is not used and mostly used for tables (don't get confused here, we don't use index in this script)
							local possibleTool5 = player2.Backpack:FindFirstChild("Sword")
							local possibleTool6 = player2.Character:FindFirstChild("Sword")

							if possibleTool5 then --if the tool is in the backpack
								possibleTool5:Destroy()
							elseif possibleTool6 then --if it's equipped
								possibleTool6:Destroy()
							end
						end
						
						player2.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocs.SpawnLocation1.CFrame
						
						plr1CurrentKills = 0
						plr2CurrentKills = 0
						
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.colon.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.timer.Visible = true
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.colon.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.timer.Visible = true
						
						part1.Transparency = 0
						part1.CanCollide = true
						part2.Transparency = 0
						part2.CanCollide = true
						
					elseif p == player2 then
						
						for i, v in ipairs(connections) do
							v:Disconnect()
						end

						player1.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1

						for index,player in pairs(Players:GetPlayers()) do --Get all the players in the game
							--player is the player we are currently at
							--index is not used and mostly used for tables (don't get confused here, we don't use index in this script)
							local possibleTool5 = player1.Backpack:FindFirstChild("Sword")
							local possibleTool6 = player1.Character:FindFirstChild("Sword")

							if possibleTool5 then --if the tool is in the backpack
								possibleTool5:Destroy()
							elseif possibleTool6 then --if it's equipped
								possibleTool6:Destroy()
							end
						end

						player1.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocs.SpawnLocation1.CFrame

						plr1CurrentKills = 0
						plr2CurrentKills = 0

						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.colon.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.timer.Visible = true
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.colon.Visible = false
						game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.timer.Visible = true

						part1.Transparency = 0
						part1.CanCollide = true
						part2.Transparency = 0
						part2.CanCollide = true
					end
					
					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)
				
			end

			connections[1] = player1.CharacterRemoving:Connect(function(hit)

				char1 = nil
				char2 = nil

				char1 = player1.Character
				char2 = player2.Character

				local player1 = game.Players:GetPlayerFromCharacter(char1)

				local humanoid1 = char1.Humanoid

				plr2CurrentKills = plr2CurrentKills + 1

				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
				
				local Sword1 = SwordGiver:Clone()
				local Sword2 = SwordGiver:Clone()

				if disconnectConnections(plr2CurrentKills) then return end -- will check the kills and disconnect connections if it's over 5

				char2.Humanoid.Health = 100

				char2.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player2Spawn.Position
				)
				char2.HumanoidRootPart.CFrame = Arena1Player2Spawn.CFrame
				
				wait(0.3)
				
				for index,player in pairs(Players:GetPlayers()) do --Get all the players in the game
					--player is the player we are currently at
					--index is not used and mostly used for tables (don't get confused here, we don't use index in this script)
					local possibleTool = player1.Backpack:FindFirstChild("NoobSign")
					local possibleTool2 = player1.Character:FindFirstChild("NoobSign")

					if possibleTool then --if the tool is in the backpack
						possibleTool:Destroy()
					elseif possibleTool2 then --if it's equipped
						possibleTool2:Destroy()
					end
				end
				
				Sword1.Parent = player1.Backpack
				
			end)
			
			connections[2] = player2.CharacterRemoving:Connect(function(hit1)

				char1 = nil
				char2 = nil

				char1 = player1.Character
				char2 = player2.Character

				local player2 = game.Players:GetPlayerFromCharacter(char2)

				local humanoid2 = char2.Humanoid

				plr1CurrentKills = plr1CurrentKills + 1

				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
				
				local Sword1 = SwordGiver:Clone()
				local Sword2 = SwordGiver:Clone()
				
				if disconnectConnections(plr1CurrentKills) then return end -- will check the kills and disconnect connections if it's over 5

				char1.Humanoid.Health = 100

				char1.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
				char1.HumanoidRootPart.CFrame = Arena1Player1Spawn.CFrame
				
				wait(0.3)
				
				for index,player in pairs(Players:GetPlayers()) do --Get all the players in the game
					--player is the player we are currently at
					--index is not used and mostly used for tables (don't get confused here, we don't use index in this script)
					local possibleTool3 = player2.Backpack:FindFirstChild("NoobSign")
					local possibleTool4 = player2.Character:FindFirstChild("NoobSign")

					if possibleTool3 then --if the tool is in the backpack
						possibleTool3:Destroy()
					elseif possibleTool4 then --if it's equipped
						possibleTool4:Destroy()
					end
				end

				Sword2.Parent = player2.Backpack
				
			end)
		end
	end		
end

Sorry its so long, its all sort of linked. I think the error came ever since I added the ‘NoobSign’ Delete for loop

Holy crap, dude. I can’t understand one line of your code, it’s just all over the place. I mean it looks like you are getting only two players in the server, when there could be more, and when something happens, the function runs, doing things for only the two players. Inside this function you have another function that is never called, that is waiting for someone to leave and then checking if it’s one of the two original players so that you can proceed to do the exact same thing, followed by another event waiting for another player to once again leave?

Your problem might be the entire function? I have no idea what to say.

1 Like

Hahahahah yeah i’m new to scripting. Basically inside the game are about 8 arenas, so I have a script for each one. I have the character.Removed functions to detect when the play dies, so i can regive them a sword, and then I have the disconnect function to teleport them back to the lobby and delete their swords. And the player.removed to make it so that if one of the players leaves while mid fight in the arena, it adds a win to the other player and teleports them back to the spawn and gets rid of their sword. Don’t worry if my script makes no sense, I’m sure i can figure out the problem. I’m just trying to get the arenas working and then ill make the script more optimised as I get better at scripting. Thanks anyway

Use this code and check out how many times each print prints itself

print("HowMany1") ---- first print
	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]
print("HowManyTimes2") ---- second print
								Player:WaitForChild("leaderstats"):WaitForChild("Wins").Value += 1
							end
						end
					end
1 Like

Strange. It’s only adding 1 all of a sudden. It printed them both once. I tested it twice and it only added 1 both times

i believe its because you essentially make multiple playerremovings in the forloop so it triggers with the same player.

could be wrong–but for each player a deathconnection function is made? or something? it is a little difficult to ascertain

1 Like

It might be because you are looping throughout a table with 3 indicies in them, try breaking the loop once it adds it. I can’t really test because I got falsely terminated so let’s hope it works.

1 Like

So, there are two connections. One for each player. They both connect to the same disconnect function. Each time a player dies, it checks whether plr1CurrentKills or plr2CurrentKills are == 5. If one of them is it disconnects the connections. Each time it checks the players kills, it also checks if either play is leaving. f they are then it runs the for loop to add to the players wins. Hopefully this makes sense, sorry if it doesn’t.

How would I break it? Thanks for the response

Like this!

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
      break
	end
  end
end

This ends the loop completely.

2 Likes

It seems to only be adding 1 now, thank you very much everyone!

1 Like

No problem <3 Have fun with your game man!

1 Like

Thank you so much, have a great day

1 Like

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