Index and value swap each other unexpected, I expected i to be the index, and v to be the value, but i sometimes becomes the value and v is the index

for i ,v in pairs (PizzaLineNpcs) do
print(i)
v.Humanoid:MoveTo(PizzaLine.Position + (DistanceBetweenNPC * (i - 1) * 2 ) )
end
1 Like
Could you post the rest of your code??
1 Like
local function npcMaker()
local npcChooser = npcs[math.random(1, #npcs)]
local npc = npcChooser:Clone()
npc.HumanoidRootPart.CFrame = spawner.CFrame
npc.Parent = workspace.NPCs
local humanoid = npc.Humanoid
table.insert(PizzaLineNpcs, npc)
--Where NPC move to before reaching the line
--humanoid:MoveTo(workspace.ConnerPath.Position)
--humanoid.MoveToFinished:Wait()
humanoid:MoveTo(workspace.DoorPath.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(PizzaLine.Position+ (DistanceBetweenNPC * (#PizzaLineNpcs - 1) * 2 ) )
end
local function npcToRegister(loc)
local npc = PizzaLineNpcs[1]
table.remove(PizzaLineNpcs,1)
npc.Humanoid:MoveTo(loc.Position)
--[[
local path = PathfindingService:CreatePath()
path:ComputeAsync(npc.HumanoidRootPart.Position, loc.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
npc.Humanoid:MoveTo(waypoint.Position)
npc.Humanoid.MoveToFinished:Wait()
end
--]]
for i ,v in pairs (PizzaLineNpcs) do
print(i)
v.Humanoid:MoveTo(PizzaLine.Position + (DistanceBetweenNPC * (i - 1) * 2 ) )
end
npcMaker()
return npc
end
That the full code that use table
table.insert support put the value in the last position right?
Is it because I am saving npc instance?

if this is easier
if InUseA then
repeat
local npc = npcToRegister(CashierAPath)
npc:Destroy()
wait(2)
until not InUseA
end
1 Like
Here is the full code
local function npcMaker()
local npcChooser = npcs[math.random(1, #npcs)]
local npc = npcChooser:Clone()
table.insert(PizzaLineNpcs, npc)
npc.HumanoidRootPart.CFrame = spawner.CFrame
npc.Parent = workspace.NPCs
local humanoid = npc.Humanoid
--Where NPC move to before reaching the line
--humanoid:MoveTo(workspace.ConnerPath.Position)
--humanoid.MoveToFinished:Wait()
humanoid:MoveTo(workspace.DoorPath.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(PizzaLine.Position+ (DistanceBetweenNPC * (#PizzaLineNpcs - 1) * 2 ) )
end
local function printtable(i,v)
print("index is " .. tostring(i) .. " and value is " .. tostring(v))
if tostring(v) == "Tester" then
return
end
print(table.unpack(v),1,#v)
end
local function npcToRegister(loc)
local Registernpc = PizzaLineNpcs[1]
table.remove(PizzaLineNpcs,1)
table.foreach(PizzaLineNpcs,printtable)
Registernpc.Humanoid:MoveTo(loc.Position)
--[[
local path = PathfindingService:CreatePath()
path:ComputeAsync(npc.HumanoidRootPart.Position, loc.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
npc.Humanoid:MoveTo(waypoint.Position)
npc.Humanoid.MoveToFinished:Wait()
end
--]]
npcMaker()
for i ,v in ipairs (PizzaLineNpcs) do
v.Humanoid:MoveTo(PizzaLine.Position + (DistanceBetweenNPC * (i - 1) * 2 ) )
end
return Registernpc
end
for i = 1,4 do
npcMaker()
end
InUseA.Changed:Connect(function(player)
if InUseA then
repeat
local Registernpc = npcToRegister(CashierAPath)
Registernpc:Destroy()
wait(2)
until not InUseA
end
end)

and this is the print statement