Spawning more npc's

Screenshot 2023-04-18 183347
So i have my script so when you step on this part it spawns a 1 npc but idk how i would change it to make it spawn 3.Also when i step on the part its super laggy

1 Like

Is there any reason this needs to be in a while true do loop?

Just remove the loop and add a debounce to the script. Also, if you want it to spawn 3 objects, use a for loop:

local debounce = false

script.Parent.Touched:Connect(function()
  if not debounce then return end
  debounce = true
  for i = 1, 3 do
    local Clone = game.ServerStorage.ClonePart:Clone()
    Clone.Parent = workspace
    Clone.Anchored = false
    task.wait()
  end
  task.wait(5) -- change this to increase/decrease cooldown time
  debounce = false
end)

Also, Clone() doesn’t take any arguments so I don’t know why you have a 1 in there. Additionally, task.wait() is preferable to wait().

2 Likes

It still spawns 1 guy though.Was i sopposed to change something

1 Like

Try this one.

-- Put it on your desired part
local DesiredNPC = game.ReplicatedStorage:FindFirstChild("NPC") --

local Part = script.Parent

local debounce = false

local function SpawnNPC(HowMany) -- I did this mainly because for the peoples that don't understand script very well
	if debounce ~= true then -- If the debounce is false then, it will spawn the NPC about exactly the "HowMany" value
		debounce = true
		
		for i = 1, HowMany do
			local newNPC = DesiredNPC:Clone() -- Apparantly clone it, like it suggest
			newNPC.Parent = workspace -- Parent it on the workspace
			newNPC.PrimaryPart.CFrame = (Part.CFrame * CFrame.new(0,2,0)) * CFrame.new(Part.CFrame.LookVector * -2) -- Make it spawn behind the part
			
			task.wait()
		end
		
		task.wait(5)
		debounce = false
	end
end

Part.Touched:Connect(function()
	SpawnNPC(3) -- You can change the number to whatever you like, for example 5 or 15
end)

Sorry if make some grammar mistakes.

1 Like