My script doesn't wait to check the connections

I use connections in my script to check for when a players kills reaches 5, but once the players kills reach 5 it seems to take a second for the script to disconnect the connections. This wouldn’t be a problem but it seems like the script keeps running the connections for a couple of seconds after I tell it to check whether it should disconnect them. E.g the player teleports and is given a sword even though it is after the part where I check the connections. Any ideas how I can fix this?

local function startEvent()
	
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.Visible = true
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.Visible = true

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()
	local char1 = player1.Character
	local char2 = player2.Character
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn

	local character1Loaded = false
	local character2Loaded = false


	print("Variables working fine")


	if char1 or char2 then


		local humanoid1 = char1:FindFirstChild("Humanoid")
		local humanoid2 = char2:FindFirstChild("Humanoid")


		if humanoid1 or 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 > 4 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end
					
					if plr1CurrentKills == 5 then
						winner = player1
					end
					if plr2CurrentKills == 5 then
						winner = player2			
					end			
					
					player1.RespawnLocation = game.Workspace.SpawnLocation
					player2.RespawnLocation = game.Workspace.SpawnLocation
					
					if winner == player1 then
						Sword1:Destroy()
						player1.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
					end

					plr1CurrentKills = 0
					plr2CurrentKills = 0
					
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

					
					part1.Transparency = 0
					part1.CanCollide = true
					part2.Transparency = 0
					part2.CanCollide = true
				end
			end

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

				local player1 = game.Players:GetPlayerFromCharacter(char1)

				local humanoid1 = player1.Character.Humanoid
				
				local humanoidRootPart1 = player1.Character:FindFirstChild("HumanoidRootPart")
				local humanoidRootPart2 = player2.Character:FindFirstChild("HumanoidRootPart")

				plr2CurrentKills = plr2CurrentKills + 1
				
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
				
				player2.Character.Humanoid.Health = 100
				
				disconnectConnections(plr2CurrentKills) -- will check the kills and disconnect connections if it's over 5
				
				wait(1)

				player2.Character.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player2Spawn.Position
				)
				player2.Character.HumanoidRootPart.CFrame = Arena1Player2Spawn.CFrame
				wait(1)
				local Sword1 = SwordGiver:Clone()
				Sword1.Parent = player1.Backpack
				
			end)
			connections[2] = player2.CharacterRemoving:Connect(function(hit)
				
				char2 = nil
				
				char2 = player2.Character
				
				local player2 = game.Players:GetPlayerFromCharacter(char2)

				local humanoid2 = player2.Character.Humanoid

				local humanoidRootPart1 = player1.Character:FindFirstChild("HumanoidRootPart")
				local humanoidRootPart2 = player2.Character:FindFirstChild("HumanoidRootPart")

				plr1CurrentKills = plr1CurrentKills + 1

				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

				disconnectConnections(plr1CurrentKills) -- will check the kills and disconnect connections if it's over 5

				player1.Character.Humanoid.Health = 100

				player1.Character.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
				player1.Character.HumanoidRootPart.CFrame = Arena1Player1Spawn.CFrame
				wait(1)
				local Sword2 = SwordGiver:Clone()
				Sword2.Parent = player2.Backpack

			end)
		end
	end		
end

The current event will keep running to completion, but it won’t run again. Make disconnectConnections return true if the connections were disconnected, then do this.

if disconnectConnections(plr1CurrentKills) then return end

and that will stop the current event from proceeding and reaching the teleport and stuff at the bottom.

1 Like

Thank you very muc, I have added those lines in but I don’t think I have done it right as the script still isn’t working. Any ideas what I did wrong?

local function startEvent()
	
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.Visible = true
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.Visible = true

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()
	local char1 = player1.Character
	local char2 = player2.Character
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn

	local character1Loaded = false
	local character2Loaded = false
	
	local checked = false


	print("Variables working fine")


	if char1 or char2 then


		local humanoid1 = char1:FindFirstChild("Humanoid")
		local humanoid2 = char2:FindFirstChild("Humanoid")


		if humanoid1 or 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 > 4 then
					for i, v in ipairs(connections) do
						v:Disconnect()
						return true
					end
					
					if plr1CurrentKills == 5 then
						winner = player1
					end
					if plr2CurrentKills == 5 then
						winner = player2			
					end			
					
					player1.RespawnLocation = game.Workspace.SpawnLocation
					player2.RespawnLocation = game.Workspace.SpawnLocation
					
					if winner == player1 then
						Sword1:Destroy()
						player1.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
					end

					plr1CurrentKills = 0
					plr2CurrentKills = 0
					
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

					
					part1.Transparency = 0
					part1.CanCollide = true
					part2.Transparency = 0
					part2.CanCollide = true
				end
			end

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

				local player1 = game.Players:GetPlayerFromCharacter(char1)

				local humanoid1 = player1.Character.Humanoid
				
				local humanoidRootPart1 = player1.Character:FindFirstChild("HumanoidRootPart")
				local humanoidRootPart2 = player2.Character:FindFirstChild("HumanoidRootPart")

				plr2CurrentKills = plr2CurrentKills + 1
				
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
				
				player2.Character.Humanoid.Health = 100
				
				disconnectConnections(plr2CurrentKills) -- will check the kills and disconnect connections if it's over 5
				if disconnectConnections(plr1CurrentKills) then return end
				player2.Character.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player2Spawn.Position
				)
				player2.Character.HumanoidRootPart.CFrame = Arena1Player2Spawn.CFrame
				wait(1)
				local Sword1 = SwordGiver:Clone()
				Sword1.Parent = player1.Backpack
			end)
			connections[2] = player2.CharacterRemoving:Connect(function(hit)
				
				char2 = nil
				
				char2 = player2.Character
				
				local player2 = game.Players:GetPlayerFromCharacter(char2)

				local humanoid2 = player2.Character.Humanoid

				local humanoidRootPart1 = player1.Character:FindFirstChild("HumanoidRootPart")
				local humanoidRootPart2 = player2.Character:FindFirstChild("HumanoidRootPart")

				plr1CurrentKills = plr1CurrentKills + 1

				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
				
				player1.Character.Humanoid.Health = 100
				
				disconnectConnections(plr1CurrentKills) -- will check the kills and disconnect connections if it's over 5
				
				if disconnectConnections(plr1CurrentKills) then return end

				player1.Character.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
				player1.Character.HumanoidRootPart.CFrame = Arena1Player1Spawn.CFrame
				wait(1)
				local Sword2 = SwordGiver:Clone()
				Sword2.Parent = player2.Backpack		
			end)
		end
	end		
end

I fixed the mistake and cleaned it up a little. You had the return true in the loop, but it should have been at the end of the disconnectConnections function so that everything there runs. I also fixed the place where you call disconnectConnections.

Here are the changes to the original code. Lines in red were removed or changed, lines in green were added or are the new changes.

code to copy
local function startEvent()
	
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.Visible = true
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.Visible = true

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()
	local char1 = player1.Character
	local char2 = player2.Character
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn

	local character1Loaded = false
	local character2Loaded = false
	

	print("Variables working fine")


	if char1 or char2 then


		local humanoid1 = char1:FindFirstChild("Humanoid")
		local humanoid2 = char2:FindFirstChild("Humanoid")


		if humanoid1 or 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 > 4 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end
					
					if plr1CurrentKills == 5 then
						winner = player1
					end
					if plr2CurrentKills == 5 then
						winner = player2			
					end			
					
					player1.RespawnLocation = game.Workspace.SpawnLocation
					player2.RespawnLocation = game.Workspace.SpawnLocation
					
					if winner == player1 then
						Sword1:Destroy()
						player1.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
					end

					plr1CurrentKills = 0
					plr2CurrentKills = 0
					
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

					
					part1.Transparency = 0
					part1.CanCollide = true
					part2.Transparency = 0
					part2.CanCollide = true
				end
				return true
			end

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

				local player1 = game.Players:GetPlayerFromCharacter(char1)

				local humanoid1 = player1.Character.Humanoid
				
				local humanoidRootPart1 = player1.Character:FindFirstChild("HumanoidRootPart")
				local humanoidRootPart2 = player2.Character:FindFirstChild("HumanoidRootPart")

				plr2CurrentKills = plr2CurrentKills + 1
				
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
				
				player2.Character.Humanoid.Health = 100
				
				if disconnectConnections(plr2CurrentKills) then return end

                wait(1)

				player2.Character.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player2Spawn.Position
				)
				player2.Character.HumanoidRootPart.CFrame = Arena1Player2Spawn.CFrame
				wait(1)
				local Sword1 = SwordGiver:Clone()
				Sword1.Parent = player1.Backpack

			end)
			connections[2] = player2.CharacterRemoving:Connect(function(hit)
				
				char2 = nil
				
				char2 = player2.Character
				
				local player2 = game.Players:GetPlayerFromCharacter(char2)

				local humanoid2 = player2.Character.Humanoid

				local humanoidRootPart1 = player1.Character:FindFirstChild("HumanoidRootPart")
				local humanoidRootPart2 = player2.Character:FindFirstChild("HumanoidRootPart")

				plr1CurrentKills = plr1CurrentKills + 1

				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
				
				if disconnectConnections(plr1CurrentKills) then return end

				player1.Character.Humanoid.Health = 100

				player1.Character.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
				player1.Character.HumanoidRootPart.CFrame = Arena1Player1Spawn.CFrame
				wait(1)
				local Sword2 = SwordGiver:Clone()
				Sword2.Parent = player2.Backpack		

			end)
		end
	end		
end
1 Like

Thank you so much, the disconnections works perfectly. The only problem is the script inside the two connections isn’t working. I’m getting the error message:

ServerScriptService.Arena1Script:149: attempt to index nil with ‘FindFirstChild’

Any ideas how I can fix this?

Edit: I just deleted those lines with the variables because I realized I didn’t even use them, I don’t get any error messages but the script still doesn’t work.