How to Respawn my NPCs?

Hey! I was wondering how I would get my NPCs to spawn on the click of a button (UI/UX).

I have the UI sorted, but I’ve tried clone: and all that and I still can’t figure it out.

It’s basically like what’s going on in Clean Up Roblox where the NPCs respawn. I want to make that kind of system. Not the game though.

If anyone can help it would be much appreciated!

TYSM

Peace :v:

1 Like

First, make sure you have your NPC(s) in ReplicatedStorage. Then place parts (any size, any shape, any colour, but make sure they’re can collide OFF, anchored ON, and transparent) throughout your world where you want your NPCs to spawn. These are going to be their “Spawn Points”. Then whenever you want to spawn an NPC somewhere, just pick one of these points randomly and move the NPC there.

Heres how cloning works:

local RS = game.ReplicatedStorage
local npc = rs.NPC -- your npc

--When you want to spawn them:
local newnpc = npc:Clone()
newnpc.Parent = workspace
newnpc.HumanoidRootPart.Position = POSITION -- The position of the point you picked

This should work, and let me know if you have any questions

1 Like

I will have about 50 different ones, does everyone need a “spawn point”. Do I put them in the actually tab (replicatedstorage), I need them to all spawn differently will this work with a GUI button?

I also want them to spawn at specific points. How would I do this. The NPCs already have a point (where they are anchored and placed). I just need them to basically “re-appear”. They don’t need to move. Bearing in mind the NPC is already dead, so how would the clone work?.

U can then change the position with current humanoidrootpart position

local RS = game.ReplicatedStorage
local npc = RS.npc   -- your npc

local newnpc = npc:Clone()
newnpc.Parent = workspace
newnpc.humanoid.Died:Connect(function()
local othernpc = npc:Clone()
othernpc.Parent = workspace
othernpc.HumanoidRootPart.Position=newnpc.HumanoidRootPart.Position
end)

Use this script if u have already placed them in position

1 Like

Where do I put this? Do I still put the NPCs in replicated storage?

Thanks for your response!

I’ve created systems that spawn multiple types of NPCs based on certain parameters before. It’s no easy feat but it is certainly possible.

After playing the game, I can deduce that the entirety of their system is client-based (local script). This makes it a lot easier to code.

The basis of our entire code is this:
(This is a LocalScript inside StarterPlayer → StarterPlayerScripts)

local REP = game:GetService("ReplicatedStorage")
local NPC = REP:WaitForChild("NPC")

local plr = game.Players.LocalPlayer

local npcGui = plr.PlayerGui:WaitForChild("NPC")
local button = npcGui.Regen

--// On Button Press //--
button.Activated:Connect(function()
	for _, repNPC in pairs(NPC:GetChildren()) do
		local npc = repNPC:Clone()
		npc.Parent = workspace
		npc:SetPrimaryPartCFrame()
	end
end)

This short piece of code simply detects when the UI button is activated. When the button is activated it iterates through the NPC folder and spawns them into the workspace.

The only problem now is deciding how to handle different CFrames for spawning.

We have a folder in ReplicatedStorage named NPC that holds the NPCs’ character models.
image

Option 1:
We could add attributes to each of these models, but assigning an attribute to each NPC while in studio is tedious.

Option 2:
We could create a LUA table inside a ModuleScript that stores information on where we want the NPCs to spawn. But this is also tedious.

Option 3:
We could add parts into the workspace that represent spawn points. Then we can add an ObjectValue to each NPC pointing towards their assigned spawn point.

I prefer Option 3, solely because it requires the least amount of work and code. It works just as well as the other two.


Working with Option 3:
Create an ObjectValue named Spawn inside each NPC. This step requires no code and is done in studio.

image

Create a Folder in workspace containing all the spawns. The names of your spawns do matter, they could all be named Part and it would still work. What matter is making sure your spawns are non-CanCollide and Anchored. Transparency is up to you.
image

How do I see LookVector?

image
Right click on the desired part and click Show Orientation Indicator

The LookVector is the direction in which the blue sphere is jutting out.
image

I recommend adding a decal pointing towards the LookVector of your parts. This makes it easier to create spawns in studio.
image

Then simply assign the ObjectValue to the spawn part you want.
image


After you’ve set all this up in studio, we can read this information with the script.

Local Script Code:

local REP = game:GetService("ReplicatedStorage")
local NPC = REP:WaitForChild("NPC")

local NPCSpawns = workspace.NPCSpawns

local plr = game.Players.LocalPlayer

local npcGui = plr.PlayerGui:WaitForChild("NPC")
local button = npcGui.Regen

--// On Button Press //--
button.Activated:Connect(function()
	for _, repNPC in pairs(NPC:GetChildren()) do
		local npcSpawn = repNPC:WaitForChild("Spawn").Value
		local npcSize = repNPC:GetExtentsSize()
		
		local npc = repNPC:Clone()
		npc.Parent = workspace
		npc:SetPrimaryPartCFrame(npcSpawn.CFrame + Vector3.new(0, npcSize.Y/2 + npcSpawn.Size.Y/2, 0))
	end
end)

With the addition of three lines of code we get the desired result:
image

We achieved this by setting the NPCs’ PrimaryParts’ (HumanoidRootPart) CFrames to that of the spawn part. We then added a Y offset to the CFrame to ensure the NPC spawns at the top of the spawn part. This keeps them from clipping through floors.

I’m leaving a rbxl file so that you can easily look at how I set my system up. Simply open the file up in your computer’s file explorer. Or go to ‘file’ in the top left of Roblox Studio and click ‘Open from File…’
SpawnNPC.rbxl (56.8 KB)

I hope this helps! :slight_smile:

3 Likes

The forum doesn’t let me edit these long posts, so here is the edit I made to the script that includes NPC detection:

Make sure you have an empty folder in workspace named NPCs. Or you can rename the folder what you like as long as you change the local npcs = workspace.NPCs line.

local REP = game:GetService("ReplicatedStorage")
local NPC = REP:WaitForChild("NPC")

local npcs = workspace.NPCs
local NPCSpawns = workspace.NPCSpawns

local plr = game.Players.LocalPlayer

local npcGui = plr.PlayerGui:WaitForChild("NPC")
local button = npcGui.Regen

--// On Button Press //--
button.Activated:Connect(function()
	for _, repNPC in pairs(NPC:GetChildren()) do
		if not npcs:FindFirstChild(repNPC.Name) then
			local npcSpawn = repNPC:WaitForChild("Spawn").Value
			local npcSize = repNPC:GetExtentsSize()

			local npc = repNPC:Clone()
			npc.Parent = npcs
			npc:SetPrimaryPartCFrame(npcSpawn.CFrame + Vector3.new(0, npcSize.Y/2 + npcSpawn.Size.Y/2, 0))
			npc.PrimaryPart.Anchored = false
		end
	end
end)

The place file I left does not reflect this change.

2 Likes

You are a freaking life saver! TYSM, you spent all that time just to help me. I really, really appreciate all you help. Going to test now. I’m sure it’ll work.

1 Like

1 thing. If I was to make more spawns and continue the numbers, would the system still work? Or is there something else to change also?

This system is modular, so it should work fine regardless of how many NPCs or spawns you have. You could even assign multiple NPCs to the same spawn if you wanted to.

The only issue that could arise would be client load. Loading 100 models on the client in a single frame is costly and could freeze some lower end systems. If you’re loading that many NPCs I recommend adding game:GetService("RunService").RenderStepped:Wait() after the npc:SetPrimaryPartCFrame() line and before the first end.

1 Like

I am having some troubles with the script.

What problems are you having with the script?

It won’t spawn them. If it does it picks a random one and spawns. It also can spawn more than 1 (but that’s nothing wrong).

Do you have your place set up exactly like the place file? And are there any errors in the console?

I don’t really have time rn. Thanks though, I have small experience so I think i can sort it another time.

1 Like

Alright, well if you do get back to this issue later on feel free to message me and I’ll see what I can do to help. :slight_smile:

2 Likes

Thanks so much for all your help!