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

how can i make this work with distnace?

local player = script.Parent.TargetPlayer.Value
while true do
	wait()
	script.Parent.Humanoid:MoveTo(player.Torso.Position)
end

this script is a simple pathfind. i want this script to work with distance so if the player is a 20 distance away from the npc then the npc will follow the player if the player is not 12 or above distance away from the npc the npc will not follow him. how would i do that on this script?

2 comments

0

is targetplayer an objectvalue that represents a player?

0

yes

1 answer

✓ Accepted answer

Alright, see if this works.

We have to use ".magnitude", which gets a number value from two positions.

local player = script.Parent.TargetPlayer.Value

distance = 20

while true do
    wait()
	
	if (player.Torso.Position - (TheNPCsTorso).Position).magnitude <= distance then

  		  script.Parent.Humanoid:MoveTo(player.Torso.Position)

	end

end

Change (TheNPCsTorso) to the pathway for your npc's torso and hopefully it will follow it.

1 comment

0

i have used this but it says there is a problem on line 8. output error = Workspace.Player's Cop.Script:5: attempt to index local 'player' (a nil value)

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