For context, I’m trying to implement a round system for a game inspired by Midnight Horrors and Slender Fortress 2. The map and NPCs have no problem spawning, but after the round ends, neither of them despawn despite printing messages of their deletion in the Output log. I’ve tried using both :Destroy()
and debris:AddItem()
.
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local repStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local roundStart = repStorage.Values.GameValues.RoundStart
local timer = repStorage.Values.GameValues.Timer
local startingRound = repStorage.Values.GameValues.RoundStarting
local gameMode = repStorage.Values.GameValues.GameMode
local bosses = repStorage.Bosses:GetDescendants()
local mapSpawned = false
local bossSpawned = false
local playersAlive = {}
local gameModes = {"Survival"} -- will add more gamemodes later
local intermissionTime = 10
local gameTime = 120
roundStart.Changed:Connect(function()
if roundStart.Value == true then
for tablePos, player in pairs(players:GetPlayers()) do
if player.Character then
player.Character.HumanoidRootPart.CFrame = game.Workspace.Glass_Houses.SpawnPart.CFrame * CFrame.new(1,5,1)
player.Character.Values.InRound.Value = true
playersAlive[tablePos] = player.Character.Name
end
end
else
for tablePos, player in pairs(players:GetPlayers()) do
if player.Character.Values.InRound.Value == true then
playersAlive[tablePos] = nil
player:loadCharacter()
player.Character.Values.InRound.Value = false
end
end
end
end)
function bossFunction()
local bosses = repStorage.Bosses:GetChildren()
local clone = bosses[math.random(1, #bosses)]:Clone()
if bossSpawned == false then
clone.HumanoidRootPart.CFrame = game.Workspace.Glass_Houses:FindFirstChild("BossSpawnPart").CFrame * CFrame.new(1,5,1)
clone.Parent = game.Workspace
bossSpawned = true
else
debris:AddItem(clone, 0)
bossSpawned = false
print("boss deleted")
end
end
function mapFunction()
local maps = repStorage.Maps:GetChildren()
local mapChoice = maps[math.random(1, #maps)]:Clone()
if mapSpawned == false then
mapChoice.Parent = game.Workspace
mapSpawned = true
else
debris:AddItem(mapChoice, 0)
print("map deleted")
mapSpawned = false
end
end
function roundFunction()
while wait() do
for i = intermissionTime, 0, -1 do
print(i)
roundStart.Value = false
timer.Value = "Intermission: " .. i
wait(1)
end
timer.Value = "Spawning Map..."
mapFunction()
mapSpawned = true
wait(4)
timer.Value = ""
gameMode.Value = gameModes[math.random(1,#gameModes)]
startingRound.Value = true
wait(6)
print("round start")
roundStart.Value = true
wait(5)
bossFunction()
--will later be put into a seperate function
if gameMode.Value == "Survival" then
for i = gameTime, 0, -1 do
if #playersAlive > 0 then
print(i)
roundStart.Value = true
timer.Value = "" .. i
wait(1)
else
break
end
end
timer.Value = ""
print("round end")
bossFunction()
wait(3)
startingRound.Value = false
roundStart.Value = false
mapFunction()
end
end
end
spawn(roundFunction)
I will refine the script later. For now, I’m mainly focused on fixing the despawn issue.