Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would i break while loop through a function?

so i am trying to make rap battle type game, but there is a while loop since its a round based game, but the problem is the players can reset, and I want to keep the reset, but the problem is how do I break the loop through a Died Event, speically since i can't break loops through functions or else it would give error saying you can only use break in loops, and if i use return to break the function it wouldnt break the loop and still continue, so i have no idea what to do,

local player1 = nil
local player2 = nil

local p1_luck = 0
local p1_vote = 0
local p2_luck = 0
local p2_vote = 0

local player = game.Players
local audio = game.Workspace.Beat

local info_ = game.ReplicatedStorage:WaitForChild("Info")
local info = info_.Value
local microphone = script.Mic

local game_going_on = false

function checkPlayer2()
	local current_player = player:GetChildren()
	for i = 1,#current_player do
		local player2Luck = game.Players:FindFirstChild(current_player[i].Name).PlayerGui:WaitForChild("Luck").Value
		if player2Luck > p2_luck and game.Players:FindFirstChild(current_player[i].Name).Name ~= player1.Name then
			p2_luck = player2Luck
			player2 = game.Players:FindFirstChild(current_player[i].Name)
			game.Players:FindFirstChild(current_player[i].Name).PlayerGui:WaitForChild("Luck").Value = 2
			print(player2)
		end
	end	
end

function checkPlayer()
	local current_player = player:GetChildren()
	for i = 1,#current_player do
		local player1Luck = game.Players:FindFirstChild(current_player[i].Name).PlayerGui:WaitForChild("Luck").Value
		if player1Luck > p1_luck then
			p1_luck = player1Luck
			player1 = game.Players:FindFirstChild(current_player[i].Name)
			game.Players:FindFirstChild(current_player[i].Name).PlayerGui:WaitForChild("Luck").Value = 2
			print(player1)
			checkPlayer2()
		end
	end	
end

function give_player_weapon(player1er, player2er, which)
	if which == 1 then
		if game.Players:FindFirstChild(player2er).Backpack:FindFirstChild("Mic") then
			game.Players:FindFirstChild(player2er).Backpack:FindFirstChild("Mic"):Destroy()
		elseif game.Workspace:FindFirstChild(player2er):FindFirstChild("Mic") then
			game.Workspace:FindFirstChild(player2er):FindFirstChild("Mic"):Destroy()
		end
		script.Mic:Clone().Parent = game.Workspace:FindFirstChild(player1er)
	elseif which == 2 then
		if game.Players:FindFirstChild(player1er).Backpack:FindFirstChild("Mic") then
			game.Players:FindFirstChild(player1er).Backpack:FindFirstChild("Mic"):Destroy()
		elseif game.Workspace:FindFirstChild(player1er):FindFirstChild("Mic") then
			game.Workspace:FindFirstChild(player1er):FindFirstChild("Mic"):Destroy()
		end
		script.Mic:Clone().Parent = game.Workspace:FindFirstChild(player2er)
	end
end

function reset()
	player1 = nil
	player2 = nil
	p1_luck = 0
	p1_vote = 0
	p2_luck = 0
	p2_vote = 0
end

function reset_vote()
	p1_vote = 0
	p2_vote = 0
end

function vote(p1, p2)
	game.ReplicatedStorage.Vote:FireAllClients(p1, p2)
	wait(5)
	game.ReplicatedStorage.VoteEnd:FireAllClients(p1, p2)
end

wait(2)

while true do
	function reset_checker(ck1, ck2)
		ck1.Changed:Connect(function()
			if ck1.Health <= 10 then
				return
			end
		end)
	end
	info_.Value = "STARTING."
	wait(0.1)
	info_.Value = "STARTING.."
	wait(0.1)
	info_.Value = "STARTING..."
	wait(0.1)
	info_.Value = "STARTING."
	wait(0.1)
	info_.Value = "STARTING.."
	wait(0.1)
	info_.Value = "STARTING..."
	wait(0.1)
	info_.Value = "Checking... Players..."
	
	repeat wait() until #player:GetChildren() >= 2
	info_.Value = "Validating the Chance!"
	wait(1)
	info_.Value = "Players Validating"
	wait(0.3)
	info_.Value = "PLAYERS FOUND!"
	wait(1)
	if not game_going_on then
		game_going_on = true
		checkPlayer()
		wait(1)
		
		local character1 = game.Workspace:FindFirstChild(player1.Name):WaitForChild("HumanoidRootPart")
			local ch1 = character1.Parent.Humanoid
		local character2 = game.Workspace:FindFirstChild(player2.Name):WaitForChild("HumanoidRootPart")
			local ch2 = character2.Parent.Humanoid
			
		reset_checker(ch1, ch2)
			
		wait(0.3)
		game.Workspace.Beat.Playing = true
		game.Workspace.Beat:Play()
		wait(1)
		character1.CFrame = game.Workspace.Player1TP.CFrame + Vector3.new(0,3,0)
		ch1.WalkSpeed = 0
		ch1.JumpPower = 0
		character2.CFrame = game.Workspace.Player2TP.CFrame + Vector3.new(0,3,0)
		ch2.WalkSpeed = 0
		ch2.JumpPower = 0
		give_player_weapon(player1.Name , player2.Name, 1)
		info_.Value = "PLAYER 1 UP"
		wait(30)
		give_player_weapon(player1.Name , player2.Name, 2)
		info_.Value = "PLAYER 2 UP"
		wait(30)
		give_player_weapon(player1.Name , player2.Name, 1)
		info_.Value = "PLAYER 1 UP"
		wait(20)
		give_player_weapon(player1.Name , player2.Name, 2)
		info_.Value = "PLAYER 2 UP"
		wait(20)
		game.Workspace.Beat:Stop()
		game.Workspace.Beat.Playing = false
		info_.Value = "..."
		wait(1)
		vote(player1.Name, player2.Name)
		
		if p1_vote > p2_vote then
			info_.Value = "PLAYER 1 WON"
			wait(3)
			info_.Value = "STARTING NEW GAME..."
			ch1.Health = 0
			wait(2)
		elseif p1_vote < p2_vote then
			info_.Value = "PLAYER 2 WON"
			wait(3)
			info_.Value = "STARTING NEW GAME..."
			ch1.Health = 0
			wait(2)
		elseif p1_vote == p2_vote then
			wait(1)
			info_.Value = "TIE ROUND BEGINNING..."
			reset_vote()
			wait(2)
			game.Workspace.Beat.Playing = true
			game.Workspace.Beat:Play()
			give_player_weapon(player1.Name , player2.Name, 1)
			info_.Value = "PLAYER 1 UP"
			wait(15)
			give_player_weapon(player1.Name , player2.Name, 2)
			info_.Value = "PLAYER 2 UP"
			wait(15)
			info_.Value = "..."
			vote()
			wait(1)
			if p1_vote > p2_vote then
				info_.Value = "PLAYER 1 WON"
				wait(3)
				info_.Value = "STARTING NEW GAME..."
				ch1.Health = 0
				wait(2)
			elseif p2_vote > p1_vote then
				info_.Value = "PLAYER 2 WON"
				wait(3)
				info_.Value = "STARTING NEW GAME..."
				ch1.Health = 0
				wait(2)
			elseif p1_vote == p2_vote then
				info_.Value = "PICKING RANDOM WINNER!"
				local winner = math.random(1,2)
				wait(2)
				if p1_vote > p2_vote then
					info_.Value = "PLAYER 1 WON"
					wait(3)
					info_.Value = "STARTING NEW GAME..."
					ch1.Health = 0
					wait(2)
				elseif p2_vote > p1_vote then
					info_.Value = "PLAYER 2 WON"
					wait(3)
					info_.Value = "STARTING NEW GAME..."
					ch1.Health = 0
					wait(2)
				end
			end
		end
	end
end

1 comment

0

Could you post the code using the Lua Code Blocks so we can help you with it?

1 answer

Make a variable called "running" and set it to true. Use it as the condition for your while loop. Inside the listener for Humanoid.Died events, set "running" to false. This will break your loop when anyone dies. Since this would make the game stop after anyone dies, however, I recommend you think of a different way to divert game flow.

3 comments

0

wait but does while loop finish first or else it wont stop, since it has to go through all those loops

0

i mean waits not loops

0

The current iteration of the loop will finish before stopping.

This discussion is preserved as it was. New answers, comments, and votes are closed.