Problem with the:Moveto function

I am very confused with what is the error of this code.
I tried several changes but the model still stands in place.

local Badguy = script.Parent
local maxdistance = 15
local BestDistance = 15
local targeted = nil
local Players = game:GetService("Players")

i = true
local function FindNearestTarget()
for i, target in ipairs(workspace:GetChildren()) do

	local pro = false
	local player = nil
	if target:FindFirstChild("Humanoid") then

		player = Players:FindFirstChild(target.name)
		if player then
	
			local distance = (target.PrimaryPart.Position - Badguy.PrimaryPart.Position).Magnitude
			if distance < BestDistance and distance < maxdistance then

				targeted = target

				BestDistance = distance
			end
		end
	end
	end
	return targeted

end
while true do
	local target = FindNearestTarget()
	if target then
		print(target)
		if target:FindFirstChild("PrimaryPart") then
			print(target:FindFirstChild("PrimaryPart"))
			local targetPosition = target.PrimaryPart
			Badguy.Humanoid:MoveTo(targetPosition.Position, target)
		end
	end
	task.wait(1)
end

Ok SO i found your issue, you are trying to find PrimaryPart inside the Character which doesn’t exist and is only a Property of the Model that is the character

Is it anchored? Is there any errors? Could we get the character model? Your post is very vague.

it isn’t anchored, no errors were present

Does it have its primary part set, like goa7 said?

The model’s primary part is the humanoidrootpart

@Waxing_Gibbous1
Yep i was correct, try this:

local Badguy = script.Parent
local maxdistance = 15
local BestDistance = 15
local targeted = nil
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

i = true
local function FindNearestTarget()
	for i, target in ipairs(workspace:GetChildren()) do

		local pro = false
		local player = nil
		if target:FindFirstChild("Humanoid") then

			player = Players:FindFirstChild(target.name)
			if player then

				local distance = (Badguy.PrimaryPart.Position- target.PrimaryPart.Position).Magnitude
				if distance < BestDistance and distance < maxdistance then

					targeted = target

					BestDistance = distance
				end
			end
		end
	end
	return targeted

end
while true do
	RunService.Heartbeat:Wait()
	local target = FindNearestTarget()
	if target then
		print(target)
		if target.PrimaryPart ~= nil then
			print(target.PrimaryPart)
			local targetPosition = target.PrimaryPart
			Badguy.Humanoid:MoveTo(targetPosition.Position)
		end
	end

end
Badguy.HumanoidRootPart:SetNetworkOwner(nil)

Could you explain this code if you won’t mind so I won’t make errors like this in the future?

1 Like

Alright, could we get the character model?

Also, try doing task.wait() instead of task.wait(1). I believe :MoveTo() only moves in small increments, unless I’m thinking of something else.

I Explained it here:

Edit:
Swap out Heartbeat with Stepped and thx for 70th Solution

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.