If a player leaves during the match, end the game and go back to the first function

So, I’m making a 1v1 system. Pretty much, it selects 2 users randomly and after they complete their entrances (not done but the function is included), they’re in a match against eachother.

The script is functional, but I totally forgot to add a detector incase a player leaves before (if selected) or during the match.

I’ve tried using a separate function, but then I’d have to add even more while statement do’s.

Could anyone help me sort this out? I’m really inexperienced when it comes to ending functions all of the sudden.

Code:

local selected1
local selected2

falls = game.Workspace.System.Falls
ongoing = false

local wrestler1position = game.Workspace.Teleporting.W1
local wrestler2position = game.Workspace.Teleporting.W2

function ForceEndMatch()
	if game.Players:GetPlayers()[selected1] == nil then
		print("Winner is "..game.Players:GetPlayers()[selected2].Name)
	end
	if game.Players:GetPlayers()[selected2] == nil then
		print("Winner is "..game.Players:GetPlayers()[selected1].Name)
	end
end

function EndMatch(winner)
	print(game.Players:GetPlayers()[winner].Name.." has won the match")
end

function StartMatch()
	--turns the match on
	ongoing = true

	spawn(function()
		while ongoing == true do
			wait()
			if falls.Wrestler1.Value == 3 and falls.Wrestler2.Value < 3 then
				EndMatch(selected1)
				ongoing = false
			elseif falls.Wrestler2.Value == 3 and falls.Wrestler1.Value < 3 then
				EndMatch(selected2)
				ongoing = false
			end
		end
	end)
	print("recognized")
	
	--first fall adjustments
	game.Workspace:WaitForChild(game.Players:GetPlayers()[selected1].Name)
	game.Workspace:WaitForChild(game.Players:GetPlayers()[selected2].Name)

	game.Players:GetPlayers()[selected1].Character.HumanoidRootPart.CFrame = CFrame.new(wrestler1position.Position)
	game.Players:GetPlayers()[selected2].Character.HumanoidRootPart.CFrame = CFrame.new(wrestler2position.Position)

	local clean = game.ServerStorage.Clean:Clone()
	clean.Parent = game.Players:GetPlayers()[selected1].Backpack
	clean.Parent = game.Players:GetPlayers()[selected2].Backpack
	
	wait(1)
	while ongoing == true do
		wait(1)
		--wrestler1 dies
		if game.Players:GetPlayers()[selected1].Character.Humanoid.Health == 0 and game.Players:GetPlayers()[selected1] ~= nil then --player died, waiting if another person dies
			print(game.Players:GetPlayers()[selected1].Name.." has died, waiting on toolstep") 
			wait(2)
			if game.Players:GetPlayers()[selected2].Character.Humanoid.Health == 0 and game.Players:GetPlayers()[selected2] ~= nil then --another player also died, toolstep
				
				print("Both players down")
				wait(5)
				
				game.Players:GetPlayers()[selected1].Character.HumanoidRootPart.CFrame = CFrame.new(wrestler1position.Position)
				game.Players:GetPlayers()[selected2].Character.HumanoidRootPart.CFrame = CFrame.new(wrestler2position.Position)
			
				local clean = game.ServerStorage.Clean:Clone()
				clean.Parent = game.Players:GetPlayers()[selected1].Backpack
				clean.Parent = game.Players:GetPlayers()[selected2].Backpack
				
			else
				
				falls.Wrestler2.Value = falls.Wrestler2.Value + 1
				print(game.Players:GetPlayers()[selected2].Name.." has gained a fall")
				wait(5)
				if ongoing == true then
					game.Players:GetPlayers()[selected1].Character.HumanoidRootPart.CFrame = CFrame.new(wrestler1position.Position)
					game.Players:GetPlayers()[selected2].Character.HumanoidRootPart.CFrame = CFrame.new(wrestler2position.Position)
				
					local clean = game.ServerStorage.Clean:Clone()
					clean.Parent = game.Players:GetPlayers()[selected1].Backpack
				end
			end
		end
		
		--wrestler2 dies
		if game.Players:GetPlayers()[selected2].Character.Humanoid.Health == 0 and game.Players:GetPlayers()[selected2] ~= nil then --player died, waiting if another person dies
			print(game.Players:GetPlayers()[selected2].Name.." has died, waiting on toolstep") 
			wait(2)
			if game.Players:GetPlayers()[selected1].Character.Humanoid.Health == 0 and game.Players:GetPlayers()[selected1] ~= nil then --another player also died, toolstep
				
				print("Both players down")
				wait(5)
				game.Players:GetPlayers()[selected1].Character.HumanoidRootPart.CFrame = CFrame.new(wrestler1position.Position)
				game.Players:GetPlayers()[selected2].Character.HumanoidRootPart.CFrame = CFrame.new(wrestler2position.Position)
			
				local clean = game.ServerStorage.Clean:Clone()
				clean.Parent = game.Players:GetPlayers()[selected1].Backpack
				clean.Parent = game.Players:GetPlayers()[selected2].Backpack
				
			else
				
				falls.Wrestler1.Value = falls.Wrestler1.Value + 1
				print(game.Players:GetPlayers()[selected1].Name.." has gained a fall")
				wait(5)
				if ongoing == true then
					game.Players:GetPlayers()[selected1].Character.HumanoidRootPart.CFrame = CFrame.new(wrestler1position.Position)
					game.Players:GetPlayers()[selected2].Character.HumanoidRootPart.CFrame = CFrame.new(wrestler2position.Position)
				
					local clean = game.ServerStorage.Clean:Clone()
					clean.Parent = game.Players:GetPlayers()[selected2].Backpack
				end
			end
		end
		
	end
	
end

function Entrances()

--entrance1
print("Entrance 1")
wait(20)
--end if a brick is touched


--entrance2
print("Entrance 2")
wait(5)
--end if a brick is touched

StartMatch()
	
end



function GetWrestlers()
	repeat
		--get all players in the server
		local players = game:GetService("Players")
		local playerlist = players:GetPlayers()
		wait(.2)
		selected1 = math.random(1,#playerlist)
		print (playerlist[selected1].Name.." picked, checking if they've been picked")
		
		--check
		if playerlist[selected1] ~= playerlist[selected2] and playerlist[selected1] ~= nil then
			print("valid for choice")
		else 
			print (playerlist[selected1].Name.." already in the match, restarting the randomizer")
		end
	until playerlist[selected1] ~= playerlist[selected2] and playerlist[selected1] ~= nil
	
	print(game.Players:GetPlayers()[selected1].Name.." is valid, inserted")
	
	
	--wrestler 2
	repeat
		--get all players in the server
		local players = game:GetService("Players")
		local playerlist = players:GetPlayers()
		wait(.2)
		selected2 = math.random(1,#playerlist)
		print (playerlist[selected2].Name.." picked, checking if they've been picked")
		
		--check
		if playerlist[selected2] ~= playerlist[selected1] and playerlist[selected2] ~= nil then
			print("valid for choice")
		else 
			print (playerlist[selected2].Name.." already in the match, restarting the randomizer")
		end
		
	until playerlist[selected2] ~= playerlist[selected1] and playerlist[selected2] ~= nil
	
	print(game.Players:GetPlayers()[selected2].Name.." is valid, inserted")
	
	print("Starting entrances")
	Entrances()
end

repeat wait(1) until game.Players.NumPlayers >= 2
GetWrestlers()

The script itself is unfinished, but this is a core-based prototype which is needed for it to function.

Any help is appreciated! :smiley:

2 Likes

make the script look at players that leave,

game.Players.PlayerRemoving:Connect(function()
if player.Name == (wrestler1.name or wrestler2.name) then
...end)

something like that

Thanks for replying, but it has to stop the function and restart the game.

I was thinking of using return but I wouldn’t know how to implement it. Any ideas?

hm… you can always continuously check if any of the players have left and then just add a break and fire the function that ends that whole ordeal

What minkmink mentioned is a good start. You have the variable ongoing to check if the loop should repeat. What you could do is move that variable outside of the scope (so outside of the StartMatch function) and then use the PlayerRemoving event to check if one of the wrestlers is leaving the game. If so, set the ongoing variable to false to end the round after the loop finishes.