Cloned NPC Won't Follow Me, But Uncloned NPCs Will

  1. What do you want to achieve?
    I am trying to clone NPCs from ServerStorage that constantly follow the character.

  2. What is the issue?
    Any cloned NPCs will not follow me, but if put the uncloned NPC that is inside of ServerStorage into Workspace, then it will follow me, which makes me think that the pathfinding script isn’t the problem.

  3. What solutions have you tried so far?
    I tried cloning said NPC from the Workspace and ReplicatedStorage, but that doesn’t work either.

Here is the script that spawns in the enemies:

local ServerStorage = game:GetService("ServerStorage")
local enemy = ServerStorage.Enemy
local enemySpawns = workspace.EnemySpawns

local function SpawnEnemy()
	local randomX = math.random(-250.404, 2)
	local randomZ = math.random(-251.654, 251.77)
	
	local cloneEnemy = enemy:Clone()
	cloneEnemy.Parent = workspace
	cloneEnemy.PrimaryPart.CFrame = CFrame.new(Vector3.new(randomX, 2.974, randomZ))
end

while true do
	wait(5)
	SpawnEnemy()
end

… And here is the pathfinding script:

local Players = game:GetService("Players")
local enemy = script.Parent
local humanoid = enemy:FindFirstChild("Humanoid")

local function onCharacterAdded(character, player)
	while player:GetAttribute("IsAlive") do
		wait()
		humanoid:MoveTo(character.PrimaryPart.Position, character)
	end
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character, player)
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)
1 Like
local ServerStorage = game:GetService("ServerStorage")
local enemy = ServerStorage:WaitForChild("Enemy")
local enemySpawns = workspace:WaitForChild("EnemySpawns")

local function SpawnEnemy()
	local randomX = math.random(-250.404, 2)
	local randomZ = math.random(-251.654, 251.77)

	local cloneEnemy = enemy:Clone()
	cloneEnemy.Parent = workspace
	cloneEnemy.PrimaryPart.CFrame = CFrame.new(Vector3.new(randomX, 2.974, randomZ))
end

while true do
	task.wait(5)
	SpawnEnemy()
end

You just needed to add a wait to allow for the enemy to fully load before being cloned.

That does not work sadly.

Minimum characters. =/

What type of script is the pathfinding script?

Both scripts are server scripts.

local players = game:GetService("Players")
local enemy = script.Parent
local enemyHumanoid = enemy:FindFirstChild("Humanoid")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Health = 99
		humanoid.HealthChanged:Connect(function(health)
			if health >= 1 then
				enemyHumanoid:MoveTo(character.PrimaryPart.Position, character)
			end
		end)
	end)
end)

is the pathfinding script in every npc? I assume it is but just looking for clarification

i’m also thinking that cloned npcs don’t follow because they seem to only move when a player joins the game, but cloned npcs have their code run after a player joins and that means that the pathfinding part never happens (unless another player joins)

all assuming i didnt misunderstand your code

I will try that idea. The pathfinding script is in every NPC.

That was the reason why the NPCs weren’t moving towards the player. The only problem is that this is a single-player game.

I don’t think you understood what they meant. Your pathfinding begins when the character spawns. so just to reiterate what was already said. The old NPCs know what to do but new ones don’t. all you need to do is create a new method of triggering this function / lines of code

The cloned npcs would worked if you reset because this event would fire

player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character, player)
	end)

Just to reiterate again they cannot detect Characters that have already been added before they have been.

i’d do something like this, replacing the entirety of onPlayerAdded()

local targetPlayer = game.Players:FindFirstChildWhichIsA("Player") --any way to just get the player

targetPlayer.CharacterAdded:connect(function(character) --for new characters, do pathfinding
   onCharacterAdded(character,targetPlayer)
end)

while player:GetAttribute("IsAlive") do --pathfinding for current character
   wait()
   humanoid:MoveTo(targetPlayer.Character.PrimaryPart.Position, targetPlayer.Character)
end

not sure if this code will actually work

I rewrote the script, and it worked!



I will put to code here just in case anyone else has this problem.

local Players = game:GetService("Players")
local enemy = script.Parent
local humanoid = enemy:FindFirstChild("Humanoid")

for _, player in ipairs(Players:GetChildren()) do
	local character = player.Character
	
	while player:GetAttribute("IsAlive") do
		wait()
		humanoid:MoveTo(character.PrimaryPart.Position, character)
	end
end