NPC stops after cloning it in workspace

  1. What do you want to achieve?
    I am trying to clone a npc and parent it to workspace

  2. What is the issue?
    It stops working

  3. What solutions have you tried so far?
    Revised and debugged the whole code. Still no clue.

NPC CODE :

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
db = false
function findNearestTorso(pos)
	local list = game.Workspace:GetChildren()
	local torso = nil
	local dist = 400
	local var1 = nil
	local human = nil
	local var2 = nil
	for x = 1, #list do
		var2 = list[x]
		if (var2.className == "Model") and (var2 ~= script.Parent) then
			var1 = var2:FindFirstChild("Torso")
			human = var2:FindFirstChild("Humanoid")
			if var1 and human and (human.Health > 0) then
				if (var1.Position - pos).Magnitude < dist then
					torso = var1
					dist = (var1.Position - pos).Magnitude
				end
			end
		end
	end
	return torso
end

function Hit(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human then
		if db == false then
			db = true
			human:TakeDamage(5)
			wait(0.7)
			db = false 
		end
	end
end

larm.Touched:Connect(Hit)
rarm.Touched:Connect(Hit)

while true do
	wait(0.1)
	local target = findNearestTorso(script.Parent.Torso.Position)
	if target then
		script.Parent.Humanoid:MoveTo(target.Position, target)
	end
end

CLONING SCRIPT:

local RS = game:GetService("ReplicatedStorage")
local npc = RS.Players:WaitForChild("NPC")

local klone = npc:Clone()
klone.Parent = workspace

Hello. Your function findNearestTorso seems to be based on a very old script. I’ve replicated this on Studio and apparently the problem is your NPC doesn’t detect anything to follow. It must be because there are simply no parts named “Torso” inside models that contain “Humanoid”. Try replacing line 14 with var1 = var2:FindFirstChild("HumanoidRootPart").

Also, you should remember to position the model where you want it to be after cloning it.

1 Like

My game only uses R6 characters.

Yes I have positioned the model I forgot to include it.

	local kloneRed = P_red:Clone()
	kloneRed.Script.Disabled = false
	kloneRed.Health.Disabled = false
	kloneRed.Animate.Disabled = false
	kloneRed.Parent = workspace.Fighters
	kloneRed:MoveTo(Vector3.new(43, 8, 38))

Oh ok, good job then.

Well, when I check this on Studio it works as expected; NPCs follow themselves. Are you sure both scripts are regular scripts?

If that’s not the issue, try increasing distto a bigger value to see if the dummy detects something.

2 Likes

Oh about this; I have three scripts.

  • Health which is a server script
  • MainScript which is a server script and controls the npc
  • animate which is local script

The animate script should be a server script since it is a workspace descendant, but that’s most likely unrelated to the bug.

Does it still fail to move when you define dist as math.huge?
If not, add print checks inside the script to see where it stops working.

  • Does the cloning script extract exist in the server script?
  • If the animation script is for the NPC, it doesn’t need to be a local script, but a regular script.

remember
If the player is controlling it, its a LocalScript.
If the server is controlling it, its a script.

@mishajones
Yeah it seems to print the statements by defining distance as math.huge.

@edenojack
Oh. My cloning script is a local script. So is that the reason why my script does not works?

That’s the bug then. To confirm it, go to the place in the Studio and press F5.

Go to Test > Simulation and switch from “Client” to “Server”. The NPC shouldn’t be there.

3 Likes

100% this is your problem.
Filtering Enabled means that anything created locally (By the Player’s Client, Via Local Scripts, etc) is not viewable to the server. If a local object is created that also contains a script, local or otherwise, it will never execute the code.

To get around this, you can create a RemoteEvent that both the Player and Server has access to, and then use that to ask the Server to create an NPC.

FilteringEnabled has created some smaller logic problems for developers, but they’re only really surface level. Once you’re used to them, they’re quite easy to spot (e.g, why my first question was about the cloning script.).

1 Like

Ah yes you are right.
Thank you.