How to break the while or for function... (idk real)

game start after intermission(60 sec)
after intermission we have choice random player (chance system) and give time to hide players
and now, we gets player counts in player alive count.
game (150sec) breaks when alive count <= 1, but i have trouble - function DONT BREAKS! Im try to make while, but too!
code:

– writen by Veronika337789

local Lobby = game.Workspace.Lobby
local Maps = game.ReplicatedStorage.Maps:GetChildren()
local Status = game.ReplicatedStorage.Status

local Players = game:GetService(“Players”)
local randomPlayer = nil
local plr = nil
aliveCount = game.Workspace.PlayerAlive.Value
Players.PlayerAdded:Connect(function(player)
plr = player

end)

local tweeenServ = game:GetService(“TweenService”)

local goal1 = {}
goal1.BackgroundTransparency = 1
local goal2 = {}
goal2.BackgroundTransparency = 0

local twiInfo = TweenInfo.new(1)

local function choosePlayerWithPercentage()
local players = Players:GetPlayers()
local totalWeight = 0

-- Calculate the total weight of all players
for _, player in ipairs(players) do
	totalWeight = totalWeight + player.Chance.Value -- Replace "Weight" with the actual property or value representing the player's weight/probability
end

-- Generate a random number between 0 and the total weight
local randomNum = math.random(0, totalWeight)

-- Iterate through the players and select one based on their weight
for _, player in ipairs(players) do
	randomNum = randomNum - player.Chance.Value -- Replace "Weight" with the actual property or value representing the player's weight/probability
	
	if randomNum <= 0 then
		-- Do something with the chosen player
		randomPlayer = player
		print("Chosen player: " .. player.Name)
		
		break
	end
end

end

while true do
local roundend = false
– Intermission
local parts = {}
local children = workspace:GetChildren()

for _, child in ipairs(children) do
	if child:IsA("BasePart") and child.Name == "DieGuy" then
		table.insert(parts, child)
	end
end

for _,child in ipairs(parts) do
	child:Destroy()
end

for i = 60, 0, -1 do
	Status.Value = "Intermission: "..i
	task.wait(1)
end


-- Map Selector

local ChosenMap = Maps[math.random(1, #Maps)]
local ClonedMap = ChosenMap:Clone()

ClonedMap.Parent = game.Workspace
Status.Value = "Map: "..ClonedMap.Name
if ClonedMap.Name == "Map1" then
	game.Lighting.TimeOfDay = 14
	ClonedMap.QuestComplitied.Value = false
	ClonedMap.SaveZone.CanTouch =false
	ClonedMap.SaveZone.CanQuery = false
	ClonedMap.SaveZone.Water.Transparency = 1
elseif ClonedMap.Name == "Map2" then
	game.Lighting.TimeOfDay = 3
	ClonedMap.PowerEnabled.Value = false
	ClonedMap.DoorQuest.CanCollide = true
elseif ClonedMap.Name == "Map3" then
	game.Lighting.TimeOfDay = 14
elseif ClonedMap.Name == "Map4" then
	game.Lighting.TimeOfDay = 8

end

task.wait(3)


-- Teleports to Map
choosePlayerWithPercentage()

local players = game.Players:GetPlayers()


randomPlayer.Role.Value = "SILLY"

local newChar = game.ReplicatedStorage.Characters.RigDEf:Clone()
local plrChar = randomPlayer.Character
newChar.PrimaryPart = newChar.HumanoidRootPart
newChar:SetPrimaryPartCFrame(plrChar.PrimaryPart.CFrame)
newChar.Name = randomPlayer.Name
if plrChar:FindFirstChild("Animate") and not newChar:FindFirstChild("Animate") then
	plrChar.Animate:Clone().Parent = newChar
end
if plrChar:FindFirstChild("Health") and not newChar:FindFirstChild("Health") then
	plrChar.Health:Clone().Parent = newChar
end
randomPlayer.Character = newChar
local rootPart = newChar:FindFirstChild("HumanoidRootPart")
local plrRoot = randomPlayer.Character:FindFirstChild("HumanoidRootPart")
if rootPart and plrRoot then
	rootPart.CFrame = plrRoot.CFrame
end
newChar.Parent = game.Workspace
for i, part in pairs(newChar:GetDescendants()) do
	if part:IsA("BasePart") then
		part.Anchored = false
	end
end
local HumanoidRootParRt = randomPlayer.Character.HumanoidRootPart

HumanoidRootParRt.CFrame = game.Workspace.TPPOINT.CFrame
game.SoundService.Menu:Pause()
game.SoundService.Round:Play()
game.SoundService.Lobby:Pause()
plr.PlayerGui.CoinsUi.Enabled = true
for _, player in ipairs(players) do
	-- Check if the player has the "Alive" role

	-- Increment the count if the player has the role
	player.PlayerGui.StatusGui.Enabled = false
	
	

end
game.Workspace.PlayerAlive.Value = game.Players.NumPlayers
print(game.Workspace.PlayerAlive.Value)
for i, Player in pairs(game.Players:GetPlayers()) do
	aliveCount += 1
	local tw = tweeenServ:Create(Player.PlayerGui.StartGame.Frame,twiInfo,goal2)
	Player.PlayerGui.StartGame.Enabled = true
	Player.PlayerGui.StartGame.Frame.Visible = true
	tw:Play()
	local Character = Player.Character
	plr = Player
	if Character and plr.Character ~= randomPlayer.Character then
		
		local HumanoidRootPart = Character.HumanoidRootPart
		plr.Chance.Value += 1
		HumanoidRootPart.CFrame = ClonedMap.TeleportPoint.CFrame
	end
	wait(2)
	local tw = tweeenServ:Create(Player.PlayerGui.StartGame.Frame,twiInfo,goal1)
	tw:Play()
	wait(2)
	Player.PlayerGui.StartGame.Enabled = false
	Player.PlayerGui.StartGame.Frame.Visible = false
end
randomPlayer.PlayerGui.CharacterSelection.CharacterSelection.Visible = true
for i =  math.random(2,45), 0, -1 do
	plr.PlayerGui.StatusGui.TextLabel.TextColor3 = Color3.new(1, 1, 1)
	Status.Value = "Players Hide Time: "..i

	

	task.wait(1)
end
for i = math.random(1,5), 0, -1 do
	plr.PlayerGui.StatusGui.TextLabel.TextColor3 = Color3.new(1, 0, 0)
	Status.Value = "Players Hide Time: "..i



	task.wait(1)
end

local human = randomPlayer.Character.HumanoidRootPart
human.CFrame = ClonedMap.TeleportPoint.CFrame
randomPlayer.Chance.Value = 1
-- Game Time
-- Get all players in the game

-- Create a variable to store the count of players with the "Alive" role




-- Loop through each player


-- Print the count of players with the "Alive" role
local l = math.random(50,250)
plr.PlayerGui.CoinsUi.Enabled = true
local players = game.Players:GetPlayers()
while wait(1) and l > 0 and aliveCount > 1 or aliveCount > 0 do
	Status.Value = "Game: "..l

	-- Check if only one player is left
	
	if #players <= 1 then


		break

	end
	
	if aliveCount <=0 then


		break

	end
	if game.Workspace:FindFirstChild(randomPlayer.Name) == nil  then
		break
	end
	l -= 1
end
game.SoundService.Menu:Pause()
game.SoundService.Round:Pause()
game.SoundService.Lobby:Play()
-- Teleports to Lobby

for i, Player in pairs(game.Players:GetPlayers()) do
	
	Player.Cash.Value += 30
	local Character = Player.Character
	aliveCount += 1
	if Character then

		local HumanoidRootPart = Character.HumanoidRootPart
		if game.Workspace:FindFirstChild(randomPlayer.Name) then
			HumanoidRootPart.CFrame = Lobby.TeleportPoint.CFrame
			randomPlayer.PlayerGui.CharacterSelection.Character.Visible = false
			randomPlayer.Role.Value = "None"

		end
		
	end
end


-- Destroys Map
if game.Workspace:FindFirstChild(randomPlayer.Name) then
	randomPlayer:LoadCharacter()
end

ClonedMap:Destroy()

end

local Players = game:GetService(“Players”)

2 Likes

return

1 Like

idk why roblox studio cant check my if functions. I`m put the print function in my if(in while). And they dont work. But return works, if i destroy all “if” functions

1 Like

you can use “break” on for loops and while loops and “return” on functions

1 Like

if i`m use it, my timer will crazy. Like a -49

1 Like

can you exactly show what part of the code doesnt work

1 Like

all code work good, but while function(first version be “for”) dont wanna check if functions

1 Like

while wait(1) and l > 0 and aliveCount > 1 or aliveCount > 0 do
Status.Value = "Game: "…l

-- Check if only one player is left

if #players <= 1 then


	break

end

if aliveCount <=0 then


	break

end
if game.Workspace:FindFirstChild(randomPlayer.Name) == nil  then
	break
end
l -= 1

end

1 Like

can you only show the part of the code that you want to modify. and please put full code into code format its hard for me to read

1 Like

ok, sorry for this. It whats i wanna modify, im need to end round when alive Count < 0

while wait(1) and l > 0 and aliveCount > 1 or aliveCount > 0 do
    Status.Value = "Game: "..l

-- Check if only one player is left

    if #players <= 1 then


	    break

    end

    if aliveCount <=0 then


	    break

     end
    if game.Workspace:FindFirstChild(randomPlayer.Name) == nil  then
	   break
    end
   l -= 1
end
1 Like

i dont see a problem. can you specify what you want to do? i dont understand your request

okay… it`s dont work… if functions dont work, while dont break. if i put in if function print, print doesnt work. Return make -count, break dont work. All easy. If i use function for, its dont will work

2 Likes

Want to know how to break it? Okay. I want you to clench your fists and start shoving them into your screen/monitor as hard as you can! (PLEASE DO NOT DO THIS, THAT WAS A JOKE)

1 Like

By the way, here’s your script.

(First part is broken kinda XD)

while wait(1) do
Status.Value = "Game: "…l

-- Check if only one player is left
if #players <= 1 then
    break
end

-- Check if no players are alive
if aliveCount <= 0 then
    break
end

-- Check if a specific player is not in the workspace
if game.Workspace:FindFirstChild(randomPlayer.Name) == nil then
    break
end

-- Decrease the counter
l -= 1

-- Check if the counter has reached 0
if l <= 0 then
    break
end

end

1 Like

It’s mainly the first part that f’d up what you’re tryna do.

1 Like