My loop only works the first couple times

Hi everyone, I have part of a script that runs every time a player1 or player2 dies. But it works perfectly fine, say if player1 keeps killing player2, but as soon as player2 kills player1, the script doesn’t work properly. So basically as soon as both players die once the script doesnt work. I have a feeling its something to do with the player losing their character when they die, but i’m not sure. Here is my script, the part that isn’t working properly is the startEvent function:

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

local part1 = workspace.Arena1Player1
local part2 = workspace.Arena1Player2

local team1 = workspace.Arena1Player1Spawn
local team2 = workspace.Arena1Player2Spawn

local touchedPart1 = game.Workspace.touchedArena1Plr1
local touchedPart2 = game.Workspace.touchedArena1Plr2

local standingOnArena1Player1 = nil
local standingOnArena1Player2 = nil

local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")

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 > 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end
					
					player1.RespawnLocation = game.Workspace.SpawnLocation
					player2.RespawnLocation = game.Workspace.SpawnLocation


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

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

				local player1 = game.Players:GetPlayerFromCharacter(char1)

				local humanoid1 = char1.Humanoid
				
				local humanoidRootPart1 = char1:FindFirstChild("HumanoidRootPart")
				local humanoidRootPart2 = char2: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

				disconnectConnections(plr2CurrentKills) -- will check the kills and disconnect connections if it's over 5
				
				char2.Humanoid.Health = 100

				char2.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player2Spawn.Position
				)
				humanoidRootPart1.CFrame *= CFrame.Angles(0, math.rad(270), 0)
				wait(1)
				local Sword1 = SwordGiver:Clone()
				Sword1.Parent = player1.Backpack
				
			end)
			connections[2] = player2.CharacterRemoving:Connect(function(hit)

				char2 = player2.Character

				local player2 = game.Players:GetPlayerFromCharacter(char2)

				local humanoid2 = char2.Humanoid

				local humanoidRootPart1 = char1:FindFirstChild("HumanoidRootPart")
				local humanoidRootPart2 = char2: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

				char1.Humanoid.Health = 100

				char1.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
				humanoidRootPart2.CFrame *= CFrame.Angles(0, math.rad(270), 0)
				wait(1)
				local Sword2 = SwordGiver:Clone()
				Sword2.Parent = player2.Backpack

			end)
		end
	end		
end

RemoteEvent.OnServerEvent:Connect(function() -- if any player presses the play button, executes event
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then
		--Hide playbutton gui
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
		--Teleports
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame = CFrame.new(team1.Position.X,team1.Position.Y,team1.Position.Z)
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame = CFrame.new(team2.Position.X,team2.Position.Y,team2.Position.Z)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(270), 0)
		local gameRunning = true
		standingOnArena1Player1.RespawnLocation = game.Workspace.Arena1Player1Spawn
		standingOnArena1Player2.RespawnLocation = game.Workspace.Arena1Player2Spawn
		
		part1.Transparency = 1
		part1.CanCollide = false
		part2.Transparency = 1
		part2.CanCollide = false
		startEvent() -- triggers function
	end
end)


-----------------------------------------------------

--				PART1 Events				 --

-----------------------------------------------------

-- Touched functions [Events now set each part that was touched to the players name instead of the opposite button]

touchedPart1.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 == nil then -- Check if not standing and if not occupied

			part1.BrickColor = BrickColor.new("Teal") -- Set color
			part1.Material = Enum.Material.Neon
			standingOnArena1Player1 = Player
		end	
	end
end)

touchedPart1.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 ~= nil then -- Check if standing and is occupied
			if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
			end
			part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
			part1.Material = Enum.Material.Plastic
			standingOnArena1Player1 = nil

			if standingOnArena1Player2 ~= nil then
				if standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

-----------------------------------------------------

--				PART2 Events				 --

-----------------------------------------------------

touchedPart2.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 == nil then -- Check if not standing and if not occupied

			part2.BrickColor = BrickColor.new("Teal") -- Set color
			part2.Material = Enum.Material.Neon
			standingOnArena1Player2 = Player
		end	
	end
end)

touchedPart2.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 ~= nil then -- Check if standing and is occupied
			if   standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
			end
			part2.BrickColor = BrickColor.new("Smoky grey") -- Set color
			part2.Material = Enum.Material.Plastic
			standingOnArena1Player2 = nil

			if standingOnArena1Player1 ~= nil then
				if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

while task.wait(0) do
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then -- checks if both players are on top
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = true
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = true
	end
end

What are you getting for your print statements?

Just a guess, but try setting your char1 and char2 inside the RemoteEvent so it runs every time, instead of at the beginning of the script.

1 Like

First off, please shorten the script, I’m not going to read 200 lines of code, but, after a quick glance, it seems that you are using a local-script to handle multiple players, which automatically should be done on the server.

Its a server script. And I included the whole script because it shows how I identified the players that are in the arena 1v1. As my issue is related to losing the variable that the player is stored as when they die.

If it’s on the server then this may be your issue:

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

Yes, I found that weird because there was someone else who edited my script and they added that, but without that my script doesn’t work. Also the part where it is used is not the part that isnt working. I don’t believe I use those variables inside the startEvent function. Thanks for the response, do you know if theres anything else I can try?

Are you using a local script or Server Script?