Making a different type of rig with a humanoid move to closest player help

This is the ‘different’ type of rig, it has a humanoid, primary part and everything is grouped in a model.
It can move if i set the MoveToPart manually but when i try it with a script it kinda breaks.
image

heres the script:

local SearchDistance = 	100000000

local WanderX, WanderZ = 30, 30

function getHumanoid(model)
   for _, v in pairs(model:GetChildren())do
   	if v:IsA'Humanoid' then
   		return v
   	end
   end
end


local Bot = script.Parent
local human = Bot:WaitForChild("Humanoid")
local hroot = Bot:WaitForChild("HumanoidRootPart")
local zspeed = hroot.Velocity.magnitude
local head = Bot.Head
local vars = script.vars

local pfs = game:GetService("PathfindingService")
local players = game:GetService('Players')

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

local path
local waypoint

local chaseName = nil

function GetTorso(part)
   local chars = game.Workspace:GetDescendants()
   local chaseRoot = nil
   local chaseTorso = nil
   local chasePlr = nil
   local chaseHuman = nil
   local mag = SearchDistance
   for i = 1, #chars do
   	chasePlr = chars[i]
   	if chasePlr:FindFirstChild("Humanoid") and not chasePlr:FindFirstChild("Enemy") and not chasePlr:FindFirstChild("Helper") then
   		chaseHuman = getHumanoid(chasePlr)
   		chaseRoot = chasePlr:FindFirstChild'HumanoidRootPart'
   		if chaseRoot ~= nil and chaseHuman ~= nil and chaseHuman.Health > 0 and not chaseHuman:FindFirstChild("Enemy") and not chasePlr:FindFirstChild("Helper") then
   			if (chaseRoot.Position - part).magnitude < mag then
   				chaseName = chasePlr.Name
   				chaseTorso = chaseRoot
   				mag = (chaseRoot.Position - part).magnitude
   			end
   		end
   	end
   end
   return chaseTorso
end

function GetPlayersBodyParts(t)
   local torso = t
   if torso then
   	local figure = torso.Parent
   	for _, v in pairs(figure:GetChildren())do
   		if v:IsA'Part' then
   			return v.Name
   		end
   	end
   else
   	return "HumanoidRootPart"
   end
end

while wait() do
   local nrstt = GetTorso(hroot.Position)
   if nrstt ~= nil and human.Health > 0 then -- if player detected	
   	vars.Wandering.Value = false
   	vars.Chasing.Value = true
   	local function checkw(t)
   		local ci = 3
   		if ci > #t then
   			ci = 3
   		end
   		if t[ci] == nil and ci < #t then
   			repeat ci = ci + 1 wait() until t[ci] ~= nil
   			return Vector3.new(1,0,0) + t[ci]
   		else
   			ci = 3
   			return t[ci]
   		end
   	end

   	path = pfs:FindPathAsync(hroot.Position, nrstt.Position)
   	waypoint = path:GetWaypoints()
   	local connection;

   	local direct = Vector3.FromNormalId(Enum.NormalId.Front)
   	local ncf = hroot.CFrame * CFrame.new(direct)
   	direct = ncf.p.unit
   	local rootr = Ray.new(hroot.Position, direct)
   	local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)

   	if path and waypoint or checkw(waypoint) then
   		if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
   			local part = Instance.new("Part")
   			part.Position = waypoint[i].Position
   			part.Size = Vector3.new(1,1,1)
   			part.Anchored = true
   			part.Parent = workspace
   		end

   		if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
   			connection = human.Changed:connect(function()
   				human.Jump = true
   			end)
   			human:MoveTo( waypoint[4].Position )
   			local part = Instance.new("Part")
   			part.Position = waypoint[i].Position
   			part.Size = Vector3.new(1,1,1)
   			part.Anchored = true
   			part.Parent = workspace
   		else
   			human.Jump = false
   		end

   		if connection then
   			connection:Disconnect()
   		end

   	else
   		for i = 3, #waypoint do
   			human:MoveTo( waypoint[i].Position )	
   			local part = Instance.new("Part")
   			part.Position = waypoint[i].Position
   			part.Size = Vector3.new(1,1,1)
   			part.Anchored = true
   			part.Parent = workspace
   		end
   	end
   	path = nil
   	waypoint = nil
   elseif nrstt == nil then
   	vars.Wandering.Value = true
   	vars.Chasing.Value = false
   	CchaseName = nil
   	path = nil
   	waypoint = nil
   	human.MoveToFinished:Wait()
   end
end

can someone help me making it work?

1 Like