Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can you tell me why it doesn't work "bad argument #2 (Vector3 expected, got Object)"?

local monster = script.Parent.Torso
local humanoid = script.Parent.Humanoid

function findTarget(pos)
 	local list = game.Workspace:children()
	local torso = nil
	local dist = 10000
	local body = nil
	local human = nil
	local body2 = nil
	for x = 1, #list do
		body2 = list[x]
		print(x)
		print(body)
		if (body2.className == "Model") and (body2 ~= script.Parent) then
			body = body2:findFirstChild("HumanoidRootPart") or body2:findFirstChild("Torso")
			human = body2:findFirstChild("Humanoid")
			if (body ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (body.Position - pos).magnitude < dist then
					torso = body
					dist = (body.Position - pos).magnitude
				end
			end
				
		end
	end
	print(body)
	return torso
end


while true do 
	wait()
	local PathfindingService = game:GetService("PathfindingService")
	local path = PathfindingService:CreatePath()
	local target = findTarget(script.Parent.Torso)
	path:ComputeAsync(monster.position, target.position)
	local waypoints = path:GetWaypoints()
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
		end
	for k, waypoint in pairs(waypoints) do
		humanoid:MoveTo(waypoint.Position)
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		humanoid.MoveToFinished:Wait()
	end
	
end

tells me Workspace.spook1.Script:19: bad argument #2 (Vector3 expected, got Object)

1 answer

✓ Accepted answer

The parameter you're inputting for findTarget is script.Parent.Torso, as shown in the following line of code:

findTarget(script.Parent.Torso)

As a result, Pos is in fact the Torso instead of the Torso position. To fix the issue, simply do .Position on the parameter you're giving it:

findTarget(script.Parent.Torso.Position)

This discussion is preserved as it was. New answers, comments, and votes are closed.