Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

getting an error for the parent property of an npc ? Please help!

So i am trying to remove the respawn loops & make them functions when the humanoid dies. this is the scripts i have

Server script inside test NPC

local spawna = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules"):WaitForChild("spawna"))
local hum = script.Parent.Undead
	
	hum.Died:Connect(function()
	spawna.Respawn(hum,script.Parent, 10) -- script.Parent is the Model 
end)

Module script for this

local spawna = {}

spawna.Respawn = function(robot, clone, spawntime)
wait(spawntime)
clone.Parent = nil
wait(5) 
robot:Destroy()
clone.Parent = game.Workspace.NPC
clone:MakeJoints()
end

return spawna

and this is the error, Please Help!

The Parent property of Dummy is locked, current parent: NULL, new parent NPC

2 comments

0

Line 5 in the module script can be removed. Setting the parent to nil is similar to the :Destroy() method, something which you don't want in this case.

0

Not the problem :(

1 answer

✓ Accepted answer

I recommend the following:

  • Keep all NPCs in a folder in Workspace
  • Make a script that keeps track of them all
  • Respawn them by default 10 seconds, or override with RespawnTime in the model (IntValue)

In a server script (presuming you followed what I said above):

local NPC_Folder = workspace:WaitForChild("NPCs")
local NPC_Children = NPC_Folder:GetChildren();

local defaultWait = 10;



function waitForDead (NPC)
	
	print("Waiting for NPC", NPC.Name, "to die")
	
	local NPC_Clone = NPC:Clone()
	
	local humanoid = NPC:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local connection;
		
		connection = humanoid.Died:Connect(function()
			print'npc died'
			local waitTime = (NPC:FindFirstChild("RespawnTime") or {}).Value or defaultWait
			delay(waitTime, function() 
				NPC:Destroy()
				NPC_Clone.Parent = NPC_Folder
				waitForDead(NPC_Clone)
			end)
			connection:disconnect()
		end)
	end
	
end

for _, NPC in pairs(NPC_Children) do
	waitForDead(NPC)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.