Npcs Dont Spawn In-Game

I have a tycoon game where you run a bank. You are supposed to help customers deposit and withdrawal money into their accounts, but on roblox it does not spawn in the customers. There are no errors regarding this issue in the error reports. I do know that the MoveToFinished() method has been known to be unreliable but I do not see how that could cause my npcs to not be spawning in entirely.

local npcFolder = ReplicatedStorage.NPCs:GetChildren()

local function generateNpc(player, bank)
	local currentlyWorking = bank.StartWorkButton.CurrentlyWorking
	local canGenerate = bank.StartWorkButton.CanGenerate
	
	if canGenerate.Value == true and currentlyWorking.Value == true then
		canGenerate.Value = false
		
		local npcNodes = bank.NpcNodes

		local success, response = pcall(function()
			local npc
			
			if playerData.returnPlayerData(player.UserId).FirstTime == true then
				npc = ReplicatedStorage.NPCs:FindFirstChild("Noob"):Clone()
			else
				local rng = math.random(1, #npcFolder)
				npc = npcFolder[rng]:Clone()
			end

			npc.Parent = bank.MyCustomers
			local bankConfigs = ServerStorage.BankConfigs:FindFirstChild(bank.Name)
			npc.Humanoid.WalkSpeed = bankConfigs.CustomerWalkSpeed.Value
			local brainScript = ReplicatedStorage.CustomerBrainScript:Clone()
			brainScript.Name = "Brain"
			brainScript.Parent = npc

			local root = npc:WaitForChild("HumanoidRootPart")
			root.CFrame = npcNodes.StartPoint.CFrame + Vector3.new(0,4,0)
			task.wait(1)
			npc:WaitForChild("Humanoid", 1):MoveTo(npcNodes.MiddlePoint.Position)
			npc:WaitForChild("Humanoid", 1).MoveToFinished:Wait()
			npc:WaitForChild("Humanoid", 1):MoveTo(npcNodes.MiddlePointLoan1.Position)
			npc:WaitForChild("Humanoid", 1).MoveToFinished:Wait()
			npc:WaitForChild("Humanoid", 1):MoveTo(npcNodes.EndPoint.Position)
			npc:WaitForChild("Humanoid", 1).MoveToFinished:Wait()
			task.wait(0.2)
			npc:WaitForChild("Brain", 1).Enabled = true
		end)
		
		if not success then
			warn(response)
		end
		
		task.wait(1)
		canGenerate.Value = true
	end
end

local function npcLeave(npc, player)
	local success, response = pcall(function()
		local bankName = player.Bank.Value
		local bank = workspace.Banks:FindFirstChild(bankName)
		local npcNodes = bank.NpcNodes
		
		if playerData.returnPlayerData(player.UserId).FirstTime == true then
			playerData.returnPlayerData(player.UserId).FirstTime = false
		end
		
		npc:WaitForChild("Humanoid", 1):MoveTo(npcNodes.MiddlePointLoan1.Position)
		npc:WaitForChild("Humanoid", 1).MoveToFinished:Wait()
		npc:WaitForChild("Humanoid", 1):MoveTo(npcNodes.MiddlePoint.Position)
		npc:WaitForChild("Humanoid", 1).MoveToFinished:Wait()
		npc:WaitForChild("Humanoid", 1):MoveTo(npcNodes.StartPoint.Position)
		npc:WaitForChild("Humanoid", 1).MoveToFinished:Wait()

		task.wait(0.1)
		npc:Destroy()

		generateNpc(player, bank)
	end)

	if not success then
		warn(response)
	end
end

npcFinished.Event:Connect(npcLeave)

beginWorkEvent.OnServerEvent:Connect(function(player, bank, clickedYes)
	if clickedYes then
		generateNpc(player, bank)
	end
end)

--Delete the npc
endWorkEvent.OnServerEvent:Connect(function(player, bank)
	task.wait(1)
	local myCustomers = bank.MyCustomers:GetChildren()
	for _, customer in myCustomers do
		customer:Destroy()
	end
end)

Solved the issue. When I had multiple people in the game and one person would complete a customer task it would be updated for all customers in the game and the CanGenerate value would get messed up. So, I made sure that only the customer specific to the player’s bank would be updated and that the CanGenerate value would always be set back to true at the end of an encounter.

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