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!!!