I'm creating a script that will get the average position of all the NPCs and the players position. But i'm not quite sure on how to do this. Heres what ive got so far.

while true do
    -- Get CharacterModels zone --
    local Char = game.Workspace:GetChildren()
    for i=1,#Char do
        if not Char[i]:FindFirstChild("Humanoid") then
            table.remove(Char, i)
        else
            print("Found NPC " .. Char[i].Name)
        end
    end
    for i=1,#Char do
        local TempCharMatrixX[i] = Char[i].HumanoidRootPart.Position.X
        local TempCharMatrixZ[i] = Char[i].HumanoidRootPart.Position.Z
    end
    for i=1,#Char do
        local CharMatrixX = CharMatrixX + TempCharMatrixX[i]
        local CharMatrixZ = CharMatrixZ + TempCharMatrixZ[i]
    end
    local CharMatrix = Vector2.new((CharMatrixX)/#CharMatrixX,(CharMatrixZ)/#CharMatrixZ)
end

My problem is creating a list of just NPCs' and Players' models.

5 comments

1

Considering that Vector3's are 4x1 matrices, you'd have to individually add up the X, Y, and Z values, get the average of each one by adding up each number of each value and dividing it by the amount of numbers for that value, and create a new matrix out of the 3 numbers you get.

0

How do I add all the values of a table together, would I have to do it with a loop or is there an easier way?

0

Using a generic for loop is going to be the easiest way to do this.

0

I still need to make a table of just the character models

1

Loop through every Instance in the Workspace (or wherever you keep your models) and check to see if a Humanoid exists as a child of each Instance. Proceed to add that Instance to the NPC table if it does indeed have a Humanoid. For players, you can use GetPlayerFromCharacter() on the instance.