Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

how can i make this work with distnace?

Asked by
Benqazx 108
8 years ago
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?

0
is targetplayer an objectvalue that represents a player? pwnd64 106 — 8y
0
yes Benqazx 108 — 8y

1 answer

Log in to vote
1
Answered by
pwnd64 106
8 years ago

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.

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) Benqazx 108 — 8y
Ad

Answer this question