Is there a way to do this, to pick out a specific item (or multiple items) from a table? Instead of doing this obnoxious loop
for Index, Var in next, Table do if Var == Something then --Code end end
Which will cause lag spikes and use up lot's of memory because this line will be used a TON, especially when removing dead zombies from a table.
Also known as an array.
local tbl = {"boo","noob","nene"} local certain = tbl[2] print(certain)
noob
An array calls a specific item within a table without looping it.
If you do not know it's index then, it will still work
local rand = math.random(1,100) local rand2 = math.random(1,100) local tbl = {} table.Insert(tbl,rand) table.Insert(tbl,rand2) if tbl[2] == 5 then print("5 has been selected.") end
It you are using an array then I would use a numeric loop it is much faster than using an iterator.
local function get(thing) for i=1, #tbl do if tbl[i] == thing then -- code end end end
Better still I would just use a key, value pair. You can also have parts as keys.
local part1 = Instance.new("Part") local part2 = Instance.new("Part") local tbl = {} tbl[part1] = "this is part 1" tbl[part2] = "this is part 2" print(tbl[part1]) tbl[part1] = nil print(tbl[part2]) print(tbl[part1])
You can use the MicroProfiler to better understand what is causing lag in your game.
Hope this helps.
What you want to do is index by a specific field. You might know this as a "dictionary" or a "hash table".
For example
players = {} for i, v in pairs(game:GetService("Players"):GetPlayers()) do players[v.Name] = v end
Now if we want to get a player from their name, instead of doing this...
targetName = "Bob" player = nil for i, v in pairs(game:GetService("Players"):GetPlayers()) do if v.Name == targetName then player = v break end end
...which is O(n)
, we can do this...
player = players[targetName]
...which is a constant time look up, regardless of how many elements are in your data structure.
It's not a great example but the idea is always the same. I'd make it more specific but you haven't given a lot to go on.