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

Why Wont This Work?

local player = script.Parent.TargetPlayer.Value
while true do
	wait()
	distance = 20
	if player.Torso.Position - script.Parent.Torso.Position.Magnitude <= distance then
		script.Parent.Humanoid:MoveTo(player.Torso.Position)
	end
end

this is a pathfind script. i want the npc to follow the player in targetplayervalue. targetplayervalue is an object value. when i play this script in a npc it does not work. how can i fix this? in output the error says Workspace.hhhh.FireScript:5: bad argument #2 to '?' (Vector3 expected, got number)

1 answer

✓ Accepted answer
player.Torso.Position - script.Parent.Torso.Position.Magnitude

Here's what this line is doing: You get the magnitude of script.Parent.Torso.Position, then subtract that number from your torso's position. This is why you get the error; you're subtracting a number from a Vector3.

What you want to do is get the magnitude of the difference between the two positions.

(player.Torso.Position - script.Parent.Torso.Position).magnitude

The parentheses cause the positions to be subtracted, then the magnitude is taken out of the result of the subtraction, giving you the distance between the points.

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