Destroying Map destroys players character

Heya devs,
I’ve recently had an issue with my round based game. I have a script that starts and ends the round, and picks a random map, then teleports all players there. But at the end, when the round finishes, i go to destroy the map, and for some reason it destroys the players character. Heres the script.

local roundTime = 20
local intermissionTime = 10
local timeLeft
local status
local roundHandler = game:GetService("ReplicatedStorage"):WaitForChild("RoundHandler")

local transitionEndedEvent = game.ReplicatedStorage.TransitionEnded

local maps = {
	"Castle",
	"Roblox Headquarters",
	"Robloxian House"
}

local mapFolder = workspace.CurrentMap

local function protectorCharLoad(plr)
	local protectorValue = plr.ProtectorCharacter
	local oldCharacter = plr.Character
	local newCharacter = game:GetService("ServerStorage"):WaitForChild("Characters").Protectors:FindFirstChild(protectorValue.Value):Clone()
	
	newCharacter.HumanoidRootPart.Anchored = false
	newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)
	
	
	plr.Character = newCharacter
	newCharacter.Parent = workspace.InGameChars.Protector
	return plr
end

local function avatarCharLoad(plr)
	local morph = game:GetService("ReplicatedStorage"):WaitForChild("Players"):FindFirstChild(plr.Name)
	local charClone = morph:Clone()
	local rootPart = charClone:FindFirstChild("HumanoidRootPart") 
	local plrRoot = plr.Character:FindFirstChild("HumanoidRootPart") 
	plr.Character = charClone
	plr.Character.Parent = workspace

	if rootPart and plrRoot then
		rootPart.Anchored = false
		rootPart.CFrame = plrRoot.CFrame
	end
end



timeLeft = intermissionTime
status = "Intermission"
task.wait(1)

while true do
	if timeLeft ~= 0 then
		timeLeft -= 1
	
	else
		if timeLeft == 0 then
			if status == "Intermission" then
				timeLeft = roundTime
				status = "Game"

			else
				timeLeft = intermissionTime
				status = "Intermission"
			end
		end
	end
	if status == "Game" and timeLeft == roundTime then
		
		local ranNum = math.random(1, #maps)
		local map = game:GetService("ServerStorage"):WaitForChild("Maps"):FindFirstChild(maps[ranNum]):Clone()
		map.Parent = workspace.CurrentMap
		
		
		local roundSpawnPart = map:WaitForChild("SpawnPart")
		
		task.wait(1)
		
		for i, v in game.Players:GetChildren() do
			v.Character:MoveTo(roundSpawnPart.Position)
			transitionEndedEvent.OnServerEvent:Wait()
			protectorCharLoad(v)
		end
	end
	
	if status == "Intermission" and timeLeft == intermissionTime then	
	
		for i, v in game.Players:GetChildren() do
			v.Character:MoveTo(workspace.SpawnLocation.Position)
			transitionEndedEvent.OnServerEvent:Wait()
			v.Character.Humanoid.Health = 0
		end
		
		for i, v in workspace.CurrentMap:GetChildren() do
			v:Destroy()
		end
		-- THIS IS WHERE THE MAP IS DESTROYED RIGHT BEFORE
		
	end	
	roundHandler:FireAllClients(timeLeft, status)
	task.wait(1)
end

Sorry its so long, any help would be much appreciated, and yes i know game.workspace is bad, i just havent had time to change it.
I can’t get any footage unfortunately, but what happens is that i can move my camera around a certain point, as though the player is invisible, but i am floating in the sky, and i cant seem to move.
Thanks in advance!

[EDIT]

I even made the script wait in the intermission after the round, and i saw that the players parent was workspace, and then i deleted the map, and it still destroyed the character!!!

maybe try using SpawnLocation Objects, Use The Enable Property For SpawnLocations and Player:LoadCharacter() instead of Directly Teleporting the Players

1 Like

I changed it so that it just kills the players, which puts them in the lobby which worked. It worked when i was teleporting into the game but not out back into the lobby.

Also it works without the destroying loop, but it doesnt get rid of the map ofc.

Hi everyone, i found out what was causing it, but i have NO idea why it was doing this.

in another script i did

mapFolder.ChildRemoved:Connect(function()
	for i, v in game.Players:GetChildren() do
		v:WaitForChild("GameTeam").Value = "Neutral"
	end
end)

Edit: It turns out i had a another script where when “GameTeam” was changed, it would put the player into a folder, inside the mapFolder, so it would also be destroyed