Object not getting re-referenced after being destroyed

Sorry for the weird phrasing, this is a weird issue in general.

I have a map system that spawns in flags that the teams collect, and in the first round it works fine, then when it gets destroyed after the map is reset then respawned, they can’t be collected anymore, which leads me to believe they aren’t getting recognized. They have the same name and everything.

Code Snippet:

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")) 

This is the part referencing the flags, it’s inside of a function that gets referenced in a while loop when the map is cloned.

Any help would be much appreciated.

Try using some prints for debuging, and maybe showing us more code?

1 Like

If you’re calling destroy on the parts then all refrences to the parts get deleted so have you tried recreating the flags after reloading the map

1 Like

Like, what do you mean, I have them in the map folder, so they go into the same spot every time.

1 Like

What code you need to see? I can show you the while loop.

while true do
	-- Intermission
	local intermissionTime = 10
	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

	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()

	-- Set the skybox of the map in the Lighting
	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 60

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

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

	-- Clean up
	destroyMap()
end	
1 Like

I mean are you calling destroy on the parts that you assign a flag to?

What does the destroy map function do?

It destroys the map, and all the flags, then when it respawns the map it respawns the flags as well.