Flag not getting recognized after first round

(this is pretty much just a reupload because I still can’t get it to work after a few months)

I’m making a capture the flag game with random maps and for some reason, after the first round, it stops working. Based on scripts and the output (it says infinite yield on the flags) I’m pretty sure that the flags get recognized the first round, and not the ones after.

Here’s the code:

local Maps = game.ReplicatedStorage.Maps:GetChildren()
local Status = game.ReplicatedStorage.Status
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local Teams = game:GetService("Teams")
local FlagService = require(script.CaptureFlagService)
local Workspace = game:GetService("Workspace")
local map = Workspace:WaitForChild("Map")

local RedTeam = Teams:WaitForChild("Red")
local BlueTeam = Teams:WaitForChild("Blue")
local GreenTeam = Teams:WaitForChild("Green")
local YellowTeam = Teams:WaitForChild("Yellow")

local teams = {}
for _, teamName in ipairs({"Red", "Blue", "Green", "Yellow", "Lobby"}) do
	local team = game.Teams:FindFirstChild(teamName) or Instance.new("Team")
	team.Name = teamName
	team.Parent = game.Teams
	table.insert(teams, team)
end

local mapFolder = Workspace:FindFirstChild("Map") or Instance.new("Folder")
mapFolder.Name = "Map"
mapFolder.Parent = Workspace

local lobbyTeleported = {} 

local function flags()
	FlagService.ScoreSound = game.ReplicatedStorage.SoundBin:WaitForChild("FlagCaptured")
	FlagService.SoundEnabled = true
	FlagService.DropType = "TouchReturn"

	local RedFlag = FlagService.new(RedTeam, workspace.Map:WaitForChild("RedFlagPad"))
	local BlueFlag = FlagService.new(BlueTeam, workspace.Map:WaitForChild("BlueFlagPad"))
	local GreenFlag = FlagService.new(GreenTeam, workspace.Map:WaitForChild("GreenFlagPad"))
	local YellowFlag = FlagService.new(YellowTeam, workspace.Map:WaitForChild("YellowFlagPad"))

	FlagService.TeamScoreActive = true
	RedFlag.ShowPlayerLocation = false

	task.delay(15, function()
		FlagService:SyncData()
	end)
end

local function assignPlayersToLobbyTeam()
	local players = Players:GetPlayers()
	for _, player in ipairs(players) do
		player.Team = teams[#teams]
	end
	wait(1) 
end

local function resetPlayers()
	local players = Players:GetPlayers()
	for _, player in ipairs(players) do
		player:LoadCharacter() 
	end
end

local function destroyMap()
	for _, obj in ipairs(mapFolder:GetChildren()) do
		obj:Destroy()
	end
end

local function teleportPlayerToLobby(player)
	if not lobbyTeleported[player.UserId] then
		local lobbySpawn = Workspace.Lobby and Workspace.Lobby:FindFirstChild("Spawn")
		if lobbySpawn then
			player.CharacterAdded:Connect(function(character)
				local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
				if humanoidRootPart then
					humanoidRootPart.CFrame = CFrame.new(lobbySpawn.Position)
					lobbyTeleported[player.UserId] = true
				end
			end)
		end
	end
end



Players.PlayerAdded:Connect(function(player)
	teleportPlayerToLobby(player)
end)

while true do

local intermissionTime = 5
	for i = intermissionTime, 0, -1 do
		Status.Value = "Intermission: " .. i
		assignPlayersToLobbyTeam()
		wait(.25) 
	end


	destroyMap()

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

	for _, mapObject in ipairs(ClonedMap:GetChildren()) do
		local newMapObject = mapObject:Clone()
		newMapObject.Parent = mapFolder
	end



	print("Map: " .. ClonedMap.Name) 
	Status.Value = "Map: " .. ClonedMap.Name

	flags()

	wait(2)


	local players = Players:GetPlayers()
	for _, player in ipairs(players) do
		local teamIndex = math.random(1, #teams - 1) 
		player.Team = teams[teamIndex]
	end

	wait(.1)

	resetPlayers()

	local mapSkybox = ClonedMap:FindFirstChild("Sky")
	if mapSkybox then
		Lighting.Sky.SkyboxBk = mapSkybox.SkyboxBk
		Lighting.Sky.SkyboxDn = mapSkybox.SkyboxDn
		Lighting.Sky.SkyboxFt = mapSkybox.SkyboxFt
		Lighting.Sky.SkyboxLf = mapSkybox.SkyboxLf
		Lighting.Sky.SkyboxRt = mapSkybox.SkyboxRt
		Lighting.Sky.SkyboxUp = mapSkybox.SkyboxUp
	end

	local timeOfDayString = ClonedMap:FindFirstChild("TimeOfDay")
	local timeOfDay = timeOfDayString and timeOfDayString.Value or "12:00:00" 

	Lighting.TimeOfDay = timeOfDay

	local roundDurationString = ClonedMap:FindFirstChild("RoundDuration")
	local roundDuration = roundDurationString and tonumber(roundDurationString.Value) or 15

	for i = roundDuration, 0, -1 do
		Status.Value = "Game: " .. i
		wait(1)
	end

	for _, player in ipairs(players) do
		teleportPlayerToLobby(player)
	end

	destroyMap()
end

There is no error in the code, I can’t figure out where it’s going wrong!

Here’s a video, the lack of collision means it can be collected:

Any help would greatly appreciated!

2 Likes