My script loops at the wrong line!

So at the last line of the code and before else, the script loops all over again, even though it is supposed to loop at the end of this script. Could anyone help me with this? I have been trying to fix this for days.

local text = game.ReplicatedStorage.Value
local Maps = game.ServerStorage:GetChildren()


while true do
     wait()
     game.ReplicatedStorage.ShowVotes:FireAllClients()
     workspace.Sound1:Play()
	for i = 30,1,-1 do
		text.Value = "Intermission "..i
		wait(1)
		if i == 1 then
			print("Picking gamemode")
            game.ReplicatedStorage.WhichGamemode.OnInvoke = function(chosen)
               print(chosen)
           if chosen == "Teams" then
                print("Teams")
                local function RandomTeams()
	            local RandomOrderTable = {}

	            for i, v in pairs(game.Players:GetPlayers()) do
		            if #RandomOrderTable == 0 then
			           table.insert(RandomOrderTable, v.Character)
		            else
			           table.insert(RandomOrderTable, math.random(1, #RandomOrderTable), v.Character)
		       end
	                end
	
	            for i, v in pairs(RandomOrderTable) do
                   if i % 5 == 0 then
                      local team = Instance.new("Team", game.Teams)
                      game.ReplicatedStorage.Teams:FireAllClients()
                   end		


		            if i % 4 == 0 then
			            --Team4
			        local team = Instance.new("Team", game.Teams)
                   
			          return
		             end
		
		            if i % 3 == 0 then
			          --Team3
			         return
		           end
		
		          if i % 2 == 0 then
			        --Team2
			       local team = Instance.new("Team", game.Teams)
			        return
		          end
		
		        if i % 1 == 0 then
			     --Team1
		        local team = Instance.new("Team", game.Teams)
			     return
		    end
	       end
         end
           else
             print("Classic")
			wait(5)
   game.Workspace.Sound1:Stop()
   local ChosenMap = Maps[math.random(1,#Maps)]:Clone()
print(ChosenMap)
ChosenMap.Parent = workspace
ChosenMap.Name = "LoadedMap"
    for _, player in pairs(game.Players:GetChildren()) do
    local char = player.Character
    char.HumanoidRootPart.CFrame = game.Workspace:WaitForChild("LoadedMap").SpawnPlayersToMap.CFrame
end
wait()
game.Workspace:WaitForChild("LoadedMap").Sound:Play()
for i = 5,1,-1 do
   text.Value = ("Round Starting in ".. i)
   wait(1)
end
for _, player in pairs(game.Players:GetChildren()) do
   local sword = game.ReplicatedStorage.ClassicSword:Clone()
   sword.Parent = player.Backpack
   sword = game.ReplicatedStorage.ClassicSword:Clone()
   sword.Parent = player.StarterGear
end
for i = 60,1,-1 do
   text.Value = (i)
   wait(1)
if i == 1 then
for _, player in pairs(game.Players:GetChildren()) do
    local sword = player.Backpack:FindFirstChild("ClassicSword")
    local sword2 = player.StarterGear:FindFirstChild("ClassicSword")
    local char = player.Character
    if sword then sword:Destroy() end
    if sword2 then sword2:Destroy() end
if char then
    local sword = char:FindFirstChild("ClassicSword")
    if sword then sword:Destroy() end
end
end
for _, player in pairs(game.Players:GetChildren()) do
   if player.Backpack:FindFirstChild("AzureSword") == true or player.StarterGear:FindFirstChild("AzureSword") then
    player.Character:FindFirstChild("AzureSword"):Destroy()
    player.Backpack:FindFirstChild("AzureSword"):Destroy()
    player.StarterGear:FindFirstChild("AzureSword"):Destroy()
end
end
   local function GetPlayerWithMostKills()
    local Current, Plr = -1, nil;

    for _, Player in ipairs(game:GetService("Players"):GetPlayers()) do --// ipairs is good for arrays; i++ order
        if (Player.leaderstats.Kills.Value > Current) then
            Plr = Player;
            Current = Player.leaderstats.Kills.Value; --// Assign it to the highest found
            
        end
    end

    return Plr, Current;
end

   local Players = game:GetService("Players")
   local player = Players.LocalPlayer    
   local PlayerWithMostKills, Kills = GetPlayerWithMostKills();
   print(PlayerWithMostKills)
   
 
-- Fetch the thumbnail
local userId = PlayerWithMostKills.UserId
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Dictionary = {
    Player = PlayerWithMostKills;
    Kills = Kills;
    Content = content;
}
ReplicatedStorage.ShowNameAndKills:FireAllClients(Dictionary);
ReplicatedStorage.ShowImage:FireAllClients()

-- Set the ImageLabel's content to the user thumbnail
for _,player in ipairs(game.Players:GetChildren()) do
local imageLabel = player.PlayerGui.MostKills.Frame.PlrImage
imageLabel.Image = content
imageLabel.Size = UDim2.new(0, 166,0, 154)
end

   wait(5)
   game.ReplicatedStorage.CloseImage:FireAllClients()
   game.Workspace:WaitForChild("LoadedMap").Sound:Stop()
   for _, player in pairs(game.Players:GetChildren()) do
   local char = player.Character
   char.HumanoidRootPart.CFrame = game.Workspace:WaitForChild("LobbySpawnPart").CFrame
end
   game.Workspace:WaitForChild("LoadedMap"):Destroy()
   for _, player in pairs(game.Players:GetChildren()) do
   if player.leaderstats.Kills.Value > 0 then
   player.leaderstats.Kills.Value = 0
end
end
end
end
end
end
end
 end
               
            end

The script is in serverscriptservice and feel free to ask questions

Can you show whats being printed and whats repetitive and whats not?

It prints everything it is supposed to, its just it repeats when it hit the end of Classic or Teams

Did you have a break or anything? Its in a While true do loop, So when it finishes it resets and does it again.

No I meant I have more code, but it gets repeated before it executes the rest of my code. Do you have any idea why, and how I can fix this?

Loops yields code, meaning that it won’t stop looping until you tell it do so (Put a “break” in it.), or when one requirement is still being met.(while something do, while something is something do, while vice-versa do.)

1 Like

Yes but I don’t want the while true do loop to end because this is a round script. So how would I fix it without messing up the round script

Can you show the entire script then. Some issues come from places you dont expect them.

There, the whole script is above, does that help?

Does anyone know why the script is looping at the wrong line???