I have no idea why this error happens. It seems the arguments are getting mixed up. Sure I can store the data in a global variable or table but I don't want to do that. This should work!!:
NPCSpawner.SpawnAll = function(npcs, self)
local angle
local position
for k,v in pairs(npcs) do
local position = NPCSpawner.Positions[v.ID].Pos
local angle = NPCSpawner.Positions[v.ID].Ang
print(position)
print(v.NPCs)
print(k)
NPCSpawner:SpawnAtPos(angle, position, v.NPCs)
end
end
NPCSpawner.SpawnAtPos = function(ang, pos, npcsToSpawn)
--print(ang)
--print(pos)
--print(npcs)
print(npcsToSpawn)
local npcs_involved = table.Copy( npcsToSpawn )
local npcs_affected = {}
This is what it prints to console:
720.855530 -2580.645020 -3647.968750
table: 0xec4b5420
Testing
720.855530 -2580.645020 -3647.968750
[ERROR] lua/includes/extensions/table.lua:29: bad argument #1 to 'pairs' (table expected, got userdata)
1. pairs - [C]:-1
2. Copy - lua/includes/extensions/table.lua:29
3. SpawnAtPos - addons/3dsimroom/lua/autorun/server/sv_npcspawner.lua:128
4. SpawnAll - addons/3dsimroom/lua/autorun/server/sv_npcspawner.lua:120
5. func - addons/3dsimroom/lua/autorun/server/sv_npcspawner.lua:103
6. unknown - lua/includes/extensions/net.lua:32
As you can see, npcsToSpawn is being passed as position. This makes no sense in my head.
the NPC table is stored as such for reference;
Example = {}
Example.NPCs = { table of NPCs}
Example.ID = ( position ID, linking to a position table (NPCSpawner.Positions) )
The problem is you are defining your function like this:
NPCSpawner.SpawnAtPos = function(ang, pos, npcsToSpawn)
--blah blah code
end
But you are calling it like this:
NPCSpawner:SpawnAtPos(angle, position, v.NPCs)
Do you know what using that colon does? It basically turns your function call into this:
NPCSpawner.SpawnAtPos(NPCSpawner, angle, position, v.NPCs)
As a rule, you should only use a colon to call a function if it was defined that way, like this:
function NPCSpawner:SpawnAtPos(ang, pos, npcsToSpawn)
--blah blah code
end
Defining it with a colon is equivalent to defining it like this:
function NPCSpawner.SpawnAtPos(self, ang, pos, npcsToSpawn)
--blah blah code
end
Sorry, you need to Log In to post a reply to this thread.