local larm = script.Parent:FindFirstChild("Left Arm") local rarm = script.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos) local list = game.Workspace:children() local torso = nil local dist = 30 --This Line Right Here local temp = nil local human = nil local temp2 = nil for x = 1, #list do temp2 = list[x] if (temp2.className == "Model") and (temp2 ~= script.Parent) then temp = temp2:findFirstChild("Torso") human = temp2:findFirstChild("Humanoid") Friend = temp2:findFirstChild("Friend") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and (Friend == nil) or (temp ~= nil) and (human ~= nil) and (human.Health > 0) and (Friend.Value ~= script.Parent.Friend.Value) then if (temp.Position - pos).magnitude < dist then torso = temp dist = (temp.Position - pos).magnitude end end end end return torso end
while true do wait(0.1) local target = findNearestTorso(script.Parent.Torso.Position) if target ~= nil then script.Parent.SCP:MoveTo(target.Position, target) end end
It's probably because the SCP is R15. Also next time, fix your answer, you should always put your code inside a code block by pressing the Lua icon. And provide some details on what you're trying to do and explain why you're doing this.
The solution is using script.Parent.HumanoidRootPart
instead of script.Parent.Torso
.
local monster = script.Parent local monsterTorso = monster:FindFirstChild("Torso") or monster:FindFirstChild("HumanoidRootPart") function findNearestTorso(pos) local list = game.Workspace:GetDescendants() local torso = nil local dist = 30 --This Line Right Here for _, descendant in ipairs(list) do if descendant:IsA("Model") and descendant ~= monster then local temp = descendant:FindFirstChild("Torso") or descendant:FindFirstChild("HumanoidRootPart") local human = descendant:FindFirstChildOfClass("Humanoid") local Friend = descendant:FindFirstChild("Friend") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then if Friend ~= nil then if Friend:IsA(monster.Friend.ClassName) then if Friend.Value ~= monster.Friend.Value then if (temp.Position - pos).Magnitude < dist then torso = temp dist = (temp.Position - pos).Magnitude end end end else if (temp.Position - pos).Magnitude < dist then torso = temp dist = (temp.Position - pos).Magnitude end end end end end return torso end while true do task.wait(0.1) local target = findNearestTorso(monsterTorso.Position) if target ~= nil then script.Parent.SCP:MoveTo(target.Position, target) end end