This is my simple recursive code for respawning npc, on first respawn everything works like a charm. But on the second one it spits out this scary error: exception while signaling: Must be a LuaSourceContainer
Is this because there are a limit to recursion on roblox? Or am i missing something?
Here is the code i used:
local NpcFolder = script.Parent.Npcs
local respawnTime = 2
local function setListener(npc)
local npcClone = npc:Clone()
npc.Destroying:Connect(function()
wait(respawnTime)
npcClone.Parent = workspace.Map.Npcs
setListener(npcClone)
end)
end
for i, npc in pairs(NpcFolder:GetChildren()) do
setListener(npc)
end
Try wrapping the inside of connection function in a Spawn(), maybe the wait() function delays the actual destroying of the old NPC.
Here is what I mean:
local function setListener(npc)
local npcClone = npc:Clone()
npc.Destroying:Connect(function()
spawn(function()
wait(respawnTime)
npcClone.Parent = workspace.Map.Npcs
setListener(npcClone)
end)
end)
end
Nope, didn’t help. I have just remade the same script but cloning npcs from ServerStorage and it worked!
The problem is somewhere inside the recursion.
Here is the code with more clear paths, still works the same:
local NpcFolder = workspace.Map.Npcs
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local respawnTime = 2
local function setListener(npc)
local npcClone = npc:Clone()
npc.Destroying:Connect(function()
spawn(function()
wait(respawnTime)
npcClone.Parent = workspace.Map.Npcs
setListener(npcClone)
end)
end)
end
for i, npc in pairs(NpcFolder:GetChildren()) do
setListener(npc)
end