I’m trying to add every player’s magnitude between an NPC by adding a new table inside one that says the player name and it’s magnitude. Example:
local NPCCharacter = script.Parent --NPC Character
local PlayerDistance = {}
for i, Player in pairs(game.Players:GetChildren()) do
local Distance = (Player.Charcter.HumanoidRootPart.Position - NPCCharacter.Position).Magnitude
--inserts value and the player it used inside PlayerDistance
end
--How PlayerDistance should look like after for loop ended
PlayerDistance = {
["Player's Username1"] = {
Username = --"Player's username"
Distance = --Player's Distance
},
["Player's Username2"] = {
Username = --"Player's username"
Distance = --Player's Distance
},
["Player's Username3"] = {
Username = --Player's username
Distance = --Player's Distance
}
}
local NPCCharacter = script.Parent
local PlayerDistance = {}
for i, Player in ipairs(game.Players:GetChildren()) do
local DistancePos = (Player.Charcter.HumanoidRootPart.Position - NPCCharacter.Position).Magnitude
PlayerDistance[Player.Name] = {
Username = Player.Name,
Distance = DistancePos
}
end
Technically, Arrays and Dictionaries are the same. Arrays are { [number]: any } instead of { [string]: any }. For example, an array with three elements {1, 2, 3} is actually {[1] = 1, [2] = 2, [3] = 3}, it’s just the syntax.