Why is my NPC spawning infinitely

This should not be even occuring,I dont even understand why is my NPC duplicating infinitely/

1 Like

Do you have any scripts inside of the NPC?

1 Like

Provide any code to assist us in helping you.

1 Like

I will check on that.Thanks. for helping

1 Like

this is the main problem,

local x = SpawnPassengersPart.Position.X
local y = 3
local z = SpawnPassengersPart.Position.Z
–Cframe only accept radian numbers
local oox = 0
local ooy = math.rad(180)
local ooz = 0

local PassengersList = {“Noob”, “Bacon”,“FrenchSoldier”}

–Adding Passengers
local function SpawnPassengers(x,y,z)
–Coordiantes
local newPassenger = nil
local maxPassengers = 20
local newZ = z + 3 – Base on how many you spawn
local newX = x + 3

if minPassengers.Value <= maxPassengers then

	repeat
		local randomPassenger = math.random(1,#PassengersList)
		task.wait(3)
		if randomPassenger == 1 then
			newPassenger =  ReplicatedStorage.Passengers.Noob:Clone()
		elseif randomPassenger == 2 then
			newPassenger = ReplicatedStorage.Passengers.Bacon:Clone()
		elseif randomPassenger == 3 then
			newPassenger = ReplicatedStorage.Passengers.FrenchSoldier:Clone()
		end

		newPassenger.Parent = game.Workspace.Passengers.HazeFirePassengers
		local randomSattion = math.random(1,6)-- change the value if there are more stations
		local CheckMark = Instance.new("StringValue")
		CheckMark.Name = "CheckStation"
		
		if randomSattion == 1 then
			CheckMark.Value = "BeastBurger"
		elseif randomSattion == 2 then
			CheckMark.Value = "FullTerminal"
		elseif randomSattion == 3 then
			CheckMark.Value = "WestBeach"
		elseif randomSattion == 4 then
			CheckMark.Value = "WesternTerminal"
		elseif randomSattion == 5 then
			CheckMark.Value = "WesternHospital"
		end
		for i, v in pairs(newPassenger:GetChildren()) do
			if v:IsA("BasePart") or v:IsA("MeshPart") then
				v.CollisionGroup = collisionPassenger
			end
		end
		--CheckMark.Value = --math.random(1,75)
		CheckMark.Parent = newPassenger


		newPassenger.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(newX,y,newZ)) * CFrame.Angles(oox,ooy,ooz)
		newZ += 3
		minPassengers.Value += 1
		CheckStationMain.Value += 1
	until minPassengers.Value == 20
elseif minPassengers >= maxPassengers then
	--wait(.1)
end

end

local function spawnFirstBatch()
local newPassenger = nil
CheckSpawn = true
repeat
task.wait(0.2)
–For RandomPassenger
local randomPassenger = math.random(1,#PassengersList)
if randomPassenger == 1 then
newPassenger = ReplicatedStorage.Passengers.Noob:Clone()
elseif randomPassenger == 2 then
newPassenger = ReplicatedStorage.Passengers.Bacon:Clone()
elseif randomPassenger == 3 then
newPassenger = ReplicatedStorage.Passengers.FrenchSoldier:Clone()
end
newPassenger.Parent = game.Workspace.Passengers.HazeFirePassengers
–For Stops
local randomSattion = math.random(1,6)-- change the value if there are more stations
local CheckMark = Instance.new(“StringValue”)
CheckMark.Name = “CheckStation”
if randomSattion == 1 then
CheckMark.Value = “HazeFire”
elseif randomSattion == 2 then
CheckMark.Value = “BeastBurger”
elseif randomSattion == 3 then
CheckMark.Value = “FullTerminal”
elseif randomSattion == 4 then
CheckMark.Value = “WestBeach”
elseif randomSattion == 5 then
CheckMark.Value = “WesternTerminal”
elseif randomSattion == 6 then
CheckMark.Value = “WesternHospital”
end
for i, v in pairs(newPassenger:GetChildren()) do
if v:IsA(“BasePart”) or v:IsA(“MeshPart”) then
v.CollisionGroup = collisionPassenger
end
end

	--CheckMark.Value = math.random(1,75)
	CheckMark.Parent = newPassenger

	z -= 3
	newPassenger.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(x,y,z)) * CFrame.Angles(oox,ooy,ooz)

	minPassengers.Value += 1
	CheckStationMain.Value += 1


until minPassengers.Value == 10

end

spawnFirstBatch()

SpawnPassengers(x,y,z)

function ClearingPassengers()

game.Workspace.Passengers.HazeFirePassengers:ClearAllChildren()
CheckSpawn = nil
minPassengers.Value = 0
CheckStationMain.Value = 0
z = SpawnPassengersPart.Position.Z
task.wait(8)
spawnFirstBatch()
SpawnPassengers(x,y,z)

end

Sensor.TouchEnded:Connect(function()

	for i, v in pairs(game.Workspace.BusCapacity:GetChildren()) do

		if v.Body.PlayerName.Value == newPlayer.Name then
			RealE500 = v
			Tested = "RealE500"
			break
		end
	end
	if RealE500 and Tested == "RealE500" then
		if (RealE500.Body.PathLane.Position.Z - Sensor.Position.Z) > distance then
			--SendTextEvent:FireAllClients()
			--CheckStop.Value = "HazeTerminal"
			ClearingPassengers()
		end	
	end

end)

–end

Look at the last part of the coding thats where it occured the infinite NPCS

wrap the code all in one so we can read it properly :sob:

1 Like

I have figured out the code already.The problem lies on the touchended being infinitely,thus NPC willbe infinitely

Imagine this is only half of it,This is the only spawning part.I still have to move them.

if u want to check it out this game.I am sorry,This is not completed