I have a game I am working on whereby NPCs spawn in at intervals, multiple different NPCs with different intervals. Everything works fine except for the fact that upon death, the body parts don’t despawn until the NPC respawns. Now, this is quite an issue when some of them don’t respawn for 5+ minutes, I don’t want the map littered with their body parts, but I am at a loss as to how to fix the issue.
This is the current script:
local ServerStorage = game:GetService(“ServerStorage”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local BossFolder = workspace:WaitForChild(“GladiatorFolder”)
local bosses = ServerStorage:WaitForChild(“Gladiators”)
local bossVar
while wait() do
wait(60)
local boss = bosses:GetChildren()[math.random(1,#bosses:GetChildren())]:Clone()
boss.Parent = BossFolder
boss:WaitForChild("Humanoid").HealthChanged:Connect(function()
if boss.Humanoid.Health <= 0 then
game.Debris:AddItem(bossVar,0)
bossVar = nil
end
end)
repeat wait(1) until boss:WaitForChild("Humanoid").Health == 0
local str = ""
game:GetService("ReplicatedStorage").Notification:FireAllClients(str)
boss:Destroy()
You were setting Debris to bossVar, and you never defined bossVar as the npc created, along with other issues in the script, test this one:
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BossFolder = workspace:WaitForChild("GladiatorFolder")
local bosses = ServerStorage:WaitForChild("Gladiators")
local Debris = game:GetService("Debris")
while wait() do
wait(5)
warn("spawn npc")
local boss = bosses:GetChildren()[math.random(1,#bosses:GetChildren())]:Clone()
boss.Parent = BossFolder
boss:WaitForChild("Humanoid").Died:Connect(function()
warn("Boss Humanoid Died")
Debris:AddItem(boss,3)
end)
local str = ""
game:GetService("ReplicatedStorage").Notification:FireAllClients(str)
end
You are doing something wrong, I tested that code I sent in my message, and works normally, NPC despawns after 3 seconds without any issue, the code is pretty simple its impossible its not working, what else you doing? cause theres something else you are doing weird