What do you want to achieve?
I am trying to clone a npc and parent it to workspace
What is the issue?
It stops working
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.
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.).