NPC Spawner just spawns for 1 time

I have found this code from another post in DevForum as a solution… But it just spawn for one time and i am getting 2 error. I will give script and output below. Can someone help?, thanks.

Script:

local humanoid = script.Parent:WaitForChild("Humanoid")

while task.wait(3) do
	if humanoid.Health <= 0 then
		local dummyBackup = game.ServerStorage.NPC1:Clone()
		script.Parent:Destroy()
		dummyBackup.Parent = workspace.VS
		dummyBackup.HumanoidRootPart.CFrame = game.Workspace.VS.Spawner.CFrame + Vector3.new(0,3.006,0)
		dummyBackup:PivotTo(CFrame.new(dummyBackup:GetPivot().Position) * CFrame.Angles(0, math.rad(-90), 0))
		dummyBackup.Name = "NPC"
	end
end

Output:

-- 03:10:43.297  Workspace.VS.NPC1.Respawn:5: Expected identifier, got 'end'  -  Studio - Respawn:5
-- 03:10:46.312  Workspace.VS.NPC.Respawn:6: attempt to index nil with 'Destroy'  -  Studio
3 Likes

Hi try to replace the loop with a connection function:

humanoid.Died:Connect(function()
	local dummyBackup = game.ServerStorage.NPC1:Clone()
	dummyBackup.Parent = workspace.VS
	dummyBackup.HumanoidRootPart.CFrame = game.Workspace.VS.Spawner.CFrame + Vector3.new(0,3.006,0)
	dummyBackup:PivotTo(CFrame.new(dummyBackup:GetPivot().Position) * CFrame.Angles(0, math.rad(-90), 0))
	dummyBackup.Name = "NPC"
	script.Parent:Destroy()
end)

But make sure the script is inside the model as this error “Workspace.VS.NPC.Respawn:6: attempt to index nil with ‘Destroy’ - Studio” may be related to where the script was.

1 Like

image
nothing happened, it just stayed like that

1 Like

image

Please check if prints:

humanoid.Died:Connect(function()
	print("NPC Died!")
	local success, dummyBackup = pcall(function()
		return game.ServerStorage.NPC1:Clone()
	end)
	if success then
		print("Success Cloned")
	end
	dummyBackup.Parent = workspace.VS
	dummyBackup.HumanoidRootPart.CFrame = game.Workspace.VS.Spawner.CFrame + Vector3.new(0,3.006,0)
	dummyBackup:PivotTo(CFrame.new(dummyBackup:GetPivot().Position) * CFrame.Angles(0, math.rad(-90), 0))
	dummyBackup.Name = "NPC"		
	script.Parent:Destroy()
end)
1 Like

Nothing printed, i don’t know what’s wrong…

The problem is the script is not running.

Well, it is strange the NPC is inside another model named “VS”
Also, the code is NPC1 but you named the “NPC” without the 1 in Studio.

Rename the NPC to NPC1, VS should be a folder right?

NPC1 is duplicated model with this one, just moved NPC1 to ServerStorage. And i named NPC1 to NPC again after clone so the other scripts i referred to NPC can work.

You must be sure the script can print, add a print before:

print("Script can print")
humanoid.Died:Connect(function()

image
:thinking:

Just changed VS model to Folder, still same result.

You are right, it is not that. Keep your organization the way it was. Can you post the entire content of respawn script?

It was the all script. There screenshots.


image

Ok remove the loop and add:

local humanoid = script.Parent:WaitForchild('Humanoid')

humanoid.Died:Connect(function()
	print("NPC Died!")
	local success, dummyBackup = pcall(function()
		return game.ServerStorage.NPC1:Clone()
	end)
	if success then
		print("Success Cloned")
	end
	dummyBackup.Parent = workspace.VS
	dummyBackup.HumanoidRootPart.CFrame = game.Workspace.VS.Spawner.CFrame + Vector3.new(0,3.006,0)
	dummyBackup:PivotTo(CFrame.new(dummyBackup:GetPivot().Position) * CFrame.Angles(0, math.rad(-90), 0))
	dummyBackup.Name = "NPC"		
	script.Parent:Destroy()
end)

print("Script running :",humanoid)

Lets see if print. When you kill the npc.


:skull: thats all

How do you spawn the NPC? The only explation is the script is not running inside the npc you killed.

1 Like

Bro, i understand why my loop not working. It destroy or idk how but it removes NPC1 in ServerStorage.

So i changed Script.Parent:Destroy() to game.Workspace.VS.NPC:Destroy()

But it still remove NPC1 or destroy it from ServerStorage.

Done Fixed, Thank you for helping man! you are the best @brenorocketstar . full code:

local humanoid = script.Parent:FindFirstChildOfClass("Humanoid")

while task.wait(3) do
	if humanoid.Health <= 0 then
		local dummyBackup = game.ServerStorage.NPC1:Clone()
		game.Workspace.VS.NPC:Destroy()
		dummyBackup.Parent = workspace.VS
		dummyBackup.HumanoidRootPart.CFrame = game.Workspace.VS.Spawner.CFrame + Vector3.new(0,3.006,0)
		dummyBackup:PivotTo(CFrame.new(dummyBackup:GetPivot().Position) * CFrame.Angles(0, math.rad(-90), 0))
		dummyBackup.Name = "NPC"
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.