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

How to make the table keep updating with the positions?

Asked by
obcdino 113
6 years ago

This script is supposed to make an NPC follow the nearest player. The script works, but I want the values to keep updating, so how would I do that?

person = {}
positions = {}

wait(1)

for i,v in pairs(game.Players:GetChildren())do
    table.insert(person, v.Character.HumanoidRootPart.Position)
end


while wait(0.1)do
    for i = 1, #person do
    local mag = (person[i] - script.Parent.Torso.Position).magnitude
    table.insert(positions, mag)
    table.sort(positions)
    script.Parent.Humanoid:MoveTo(person[1])
    end
end

1 answer

Log in to vote
0
Answered by 6 years ago

Since you're using the MoveTo() method for humanoids, you could just insert a reference the Torso part of the players into the table.

Ad

Answer this question