generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: for loop behaving inconsistently

Previous Thread :: Next Thread 
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 09:17 PM
so this is a section of code that is in two scripts I have (one is an edited version of the other):


print("Checks: ", #tocheck)
for i, v in pairs(tocheck) do
print("Hi")


That part of code is the same in both scripts, except when I run the scripts, one of them will output "Checks: 0" at certain times then output "Hi" right after that despite the size of the table being 0. My other script will print "Checks: 0" then not print "Hi" (like I think it should, but the edits I made assumed that it would). Is there anything I should look for in either of my scripts that could cause this?
Report Abuse
1Topcop is not online. 1Topcop
Joined: 09 Jun 2009
Total Posts: 6635
06 Jan 2013 09:20 PM
Then you're doing something very wrong. But this is not enough code to go by for us to fix it.
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 09:26 PM
I'm not asking you to fix it, I'm asking if there is anything to look for that could cause this. I can't give you useful code to "fix it" if I don't know what to look for (it's a lot of code).
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 09:27 PM
and it's obvious I was doing something very wrong >.<. That's why I came here.
Report Abuse
Jonah13249 is not online. Jonah13249
Joined: 09 Oct 2009
Total Posts: 2105
06 Jan 2013 09:28 PM
You know, there's this thing called "output"... I think it can tell you were it went wrong. And plus, you need an end in your for loop.


~ṡсɾïρτïṉģ hεlρεɾṡ ۩ lυαlεαɾṉεɾṡ ④ øƒвќṃṿј~ ღ ▂▃▅▆█ρεώḋïερïε☄сυτïερïε█▆▅▃▂ღ 【▬】
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 09:30 PM
>.< I told you the output and I said that's a _Section_ of the code, the end exists but is not important to solving the problem because the problem is that the for loop shouldn't be executing at all but it is in one of my scripts and I need to find out why and I'm wondering if anybody knows enough about the lua language to know why this for loop could be executing.
Report Abuse
Jonah13249 is not online. Jonah13249
Joined: 09 Oct 2009
Total Posts: 2105
06 Jan 2013 09:38 PM
Oh, I don't know. We could TOTALLY fix a 3 line code that could show that it's not working. Why don't you do us a favor and please, for goodness sake, show the rest of the code so we know what is going on rather than having to assume what is wrong with the problem.

~ṡсɾïρτïṉģ hεlρεɾṡ ۩ lυαlεαɾṉεɾṡ ④ øƒвќṃṿј~ ღ ▂▃▅▆█ρεώḋïερïε☄сυτïερïε█▆▅▃▂ღ 【▬】
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 09:42 PM
Trust me, showing you the rest of the code would not be doing you a favor. I just need to know reasons the for loop could sometimes run and sometimes not run despite the size of the table being zero.
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 09:43 PM
And for the last time, I'm NOT asking you to "Fix" my code.
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 09:48 PM
Basically I need some in-depth knowledge about tables in RBX.lua. For example, could there be certain times when using #table will return 0 despite having objects in the table (maybe the first index not existing or something)?
Report Abuse
doombringer42 is not online. doombringer42
Joined: 13 Nov 2007
Total Posts: 5445
06 Jan 2013 09:48 PM
if you're using metatables, then yes.

¬ LuaLearners Elite/Writer
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 09:50 PM
I haven't touched metatables
Report Abuse
doombringer42 is not online. doombringer42
Joined: 13 Nov 2007
Total Posts: 5445
06 Jan 2013 09:53 PM
well, then, I have no idea. i'm gonna have to agree with Jonah :\

¬ LuaLearners Elite/Writer
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 09:55 PM
Does using getn behave differently? I'm going to try testing with that before I post both the scripts (I would be astonished if someone found the problem in the code be meh, it happens).
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 10:05 PM
Yea getn isn't any different. I'll post the code, have fun. This post will have the non edited version, the next will have the edited one. Sorry when I made this script I hadn't started tabbing with spaces, so you're gonna have ugly non-tabyness.







function CalcMoves(map, px, py, tx, ty)
if map[ty][tx] ~= 0 then
return nil
end
local openlist, closedlist, closed, listk, closedk, tempH, tempG, xsize, ysize, curbase = {}, {}, {}, 1, 0, math.abs(px - tx) + math.abs(py - ty), 0, #map[1], #map, {}
openlist[1] = {x = px, y = py, g = 0, h = tempH, f = 0 + tempH ,par = 1} --add starting position to openlist
while listk > 0 do
wait()
closedk = closedk + 1
curbase = openlist[1] --set the current node to be the node lowest f-score (guessed traveling distance) node
table.insert(closedlist, closedk, curbase) --put current node in the closed list
if not closed[curbase.x] then
table.insert(closed, curbase.x, {})
end
table.insert(closed[curbase.x], curbase.y, true) --not my favorite way of doing things, but the only thing I can think of that could make things much faster later on
print (listk)
if curbase.x == tx and curbase.y == ty then
return closedlist
end
openlist[1] = openlist[listk]
table.remove(openlist, listk)
listk = listk - 1
local v = 1
while true do
local u = v --
if 2 * u + 1 <= listk then --
if openlist[u].f >= openlist[2 * u].f then --
v = 2 * u --
end
if openlist[v].f >= openlist[2 * u + 1].f then --min-max heap keeps smallest f-score in the first index of openlist, other min-max heap stuff marked with <>
v = 2 * u + 1
end --
elseif 2 * u <= listk then --
if openlist[u].f >= openlist[2 * u].f then --
v = 2 * u --
end
end
if u ~= v then
local temp = openlist[u]
openlist[u] = openlist[v]
openlist[v] = temp
else
break
end
end

local tocheck = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}} --[1]Right, [2]Left, [3]Down, [4]Up, [5]UpLeft, [6]UpRight, [7]DownLeft, [8]DownRight
for i, v in pairs(tocheck) do
local a = curbase.x + v[1]
local b = curbase.y + v[2]
if a > xsize or a < 1 or b > ysize or b < 1 or map[b][a] ~= 0 or (closed[a] and closed[a][b]) then
tocheck[i] = nil --don't check walls, nodes outside the map, or nodes in the closed list
end
end
--[[if closedk > 0 then
for k = 1, closedk do
for i, v in pairs(tocheck) do
if closedlist[k].x == curbase.x + v[1] and closedlist[k].y == curbase.y + v[2] then
tocheck[i] = nil -- don't check nodes allready in the closed list (I found this loop to be big source of lag, working on way to speed it up but for now I'm using an alternative method with another table.
end
end
end
end]]
print("Checks: ", #tocheck)
local myG = curbase.g
for k = 1, listk do
for i, v in pairs(tocheck) do
local tempG = myG + (math.abs(v[1]) ~= math.abs(v[2]) and 1 or 1.4)
if openlist[k].x == curbase.x + v[1] and openlist[k].y == curbase.y + 1 and openlist[k].g > tempG then
tempH = math.abs((curbase.x + v[1])-tx) + math.abs((curbase.y + v[1])-ty)
openlist[k] = {x = curbase.x + v[1], y = curbase.y + v[2], g = tempG, h = tempH, f = tempG + tempH, par = closedk}
local m = k
while m ~= 1 do --<>
if openlist[m].f <= openlist[math.floor(m/2)].f then
temp = openlist[math.floor(m/2)]
openlist[math.floor(m/2)] = openlist[m]
openlist[m] = temp
m = math.floor(m/2)
else
break
end
end
tocheck[i] = nil
end
end
end
print("Checks: ", #tocheck)
for i, v in pairs(tocheck) do
print("Hi")
local tempG = myG + (math.abs(v[1]) ~= math.abs(v[2]) and 1 or 1.4)
listk = listk + 1
tempH = math.abs((curbase.x + v[1]) - tx) + math.abs((curbase.y + v[2]) - ty)
table.insert(openlist, listk, {x = curbase.x + v[1], y = curbase.y + v[2], g = tempG, h = tempH, f = tempG+tempH, par = closedk})
m = listk
while m ~= 1 do --<>
if openlist[m].f <= openlist[math.floor(m/2)].f then
temp = openlist[math.floor(m/2)]
openlist[math.floor(m/2)] = openlist[m]
openlist[m] = temp
m = math.floor(m/2)
else
break
end
end
end
end
return nil
end


function CalcPath(closedlist)

if closedlist == nil or table.getn(closedlist) == 1 then
return nil
end
local path = {}
local pathIndex = {}
local last = #closedlist
table.insert(pathIndex,1,last)

local i = 1
while pathIndex[i] > 1 do
i = i + 1
table.insert(pathIndex, i, closedlist[pathIndex[i - 1]].par)
end

for n = #pathIndex - 1, 1, -1 do
table.insert(path, {x = closedlist[pathIndex[n]].x, y = closedlist[pathIndex[n]].y})
end

closedlist = nil
return path
end
-- note: in the examplemap, y goes down from the top.
--example:
local examplemap = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1},
{1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,0,1,0,1,1,1},
{1,1,1,1,1,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,0,1,1,1,1},
{1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1},
{1,1,1,0,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1},
{1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1},
{1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1},
{0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1},
{0,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,0,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}
for i, v in pairs(CalcPath(CalcMoves(examplemap, 1, 1, 1, 25))) do print(v["x"], v["y"]) end
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 10:08 PM
And now for the edited version. Note I'm not done editing and the commented out stuff is commented out when I test it. There are probably other problems with the code but I'm just asking why it's not running the for loop in this one, and it is running the for loop in the other one.


function CalcMoves(map, px, py, tx, ty)
if map[ty][tx] ~= 0 then
return nil
end
local openlist, closedlist, whichlist, listk, closedk, tempH, tempG, xsize, ysize, curbase = {}, {}, {}, 1, 0, math.abs(px - tx) + math.abs(py - ty), 0, #map[1], #map, {}
openlist[1] = {x = px, y = py, g = 0, h = tempH, f = 0 + tempH ,par = 1} --add starting position to openlist
table.insert(whichlist, px, {})
table.insert(whichlist[px], py, 1)
while listk > 0 do
wait()
closedk = closedk + 1
curbase = openlist[1] --set the current node to be the node lowest f-score (guessed traveling distance) node
table.insert(closedlist, closedk, curbase) --put current node in the closed list
whichlist[curbase.x][curbase.y] = -closedk --not my favorite way of doing things, but the only thing I can think of that could make things much faster later on
print ("Listk: ", listk)
if curbase.x == tx and curbase.y == ty then
print "yay"
return closedlist
end
openlist[1] = openlist[listk]
whichlist[openlist[1].x][openlist[1].y] = 1
table.remove(openlist, listk)
listk = listk - 1
local v = 1
while true do
local u = v --
if 2 * u + 1 <= listk then --
if openlist[u].f >= openlist[2 * u].f then --
v = 2 * u --
end
if openlist[v].f >= openlist[2 * u + 1].f then --min-max heap keeps smallest f-score in the first index of openlist, other min-max heap stuff marked with <>
v = 2 * u + 1
end --
elseif 2 * u <= listk and openlist[u].f >= openlist[2 * u].f then --
v = 2 * u --
end
if u ~= v then
local temp = openlist[u]
openlist[u] = openlist[v]
openlist[v] = temp
whichlist[temp.x][temp.y] = v
whichlist[openlist[u].x][openlist[u].y] = u
else
break
end
end

local tocheck = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}} --[1]Right, [2]Left, [3]Down, [4]Up, [5]UpLeft, [6]UpRight, [7]DownLeft, [8]DownRight
for i, v in pairs(tocheck) do
local a = curbase.x + v[1]
local b = curbase.y + v[2]
if a > xsize or a < 1 or b > ysize or b < 1 or map[b][a] ~= 0 or (whichlist[a] and whichlist[a][b] and whichlist[a][b] < 0) then
tocheck[i] = nil --don't check walls, nodes outside the map, or nodes in the closed list
end
end
--[[if closedk > 0 then
for k = 1, closedk do
for i, v in pairs(tocheck) do
if closedlist[k].x == curbase.x + v[1] and closedlist[k].y == curbase.y + v[2] then
tocheck[i] = nil -- don't check nodes allready in the closed list (I found this loop to be big source of lag, working on way to speed it up but for now I'm using an alternative method with another table.
end
end
end
end]]
local myG = curbase.g
--[[for i, v in pairs(tocheck) do
local a, b = curbase.x + v[1], curbase.y + v[2]
local tempG = myG + (math.abs(v[1]) ~= math.abs(v[2]) and 1 or 1.4)
if whichlist[a][b] > 0 and openlist[whichlist[a][b]]--[[.g > tempG then
tempH = math.abs(a-tx) + math.abs(b-ty)
table.insert(openlist, whichlist[a][b], {x = a, y = b, g = tempG, h = tempH, f = tempG + tempH, par = closedk})
local m = whichlist[a][b]
while m ~= 1 do --<>
if openlist[m].f <= openlist[math.floor(m/2)].f then
temp = openlist[math.floor(m/2)]
openlist[math.floor(m/2)] = openlist[m]
openlist[m] = temp
m = math.floor(m/2)
else
break
end
end
tocheck[i] = nil
end
end--]]
print("Checks: ", #tocheck)
for k = 1, listk do
for i, v in pairs(tocheck) do
local tempG = myG + (math.abs(v[1]) ~= math.abs(v[2]) and 1 or 1.4)
if openlist[k].x == curbase.x + v[1] and openlist[k].y == curbase.y + v[2] and openlist[k].g > tempG then
tempH = math.abs((curbase.x + v[1])-tx) + math.abs((curbase.y + v[1])-ty)
openlist[k] = {x = curbase.x + v[1], y = curbase.y + v[2], g = tempG, h = tempH, f = tempG + tempH, par = closedk}
local m = k
while m ~= 1 do --<>
if openlist[m].f <= openlist[math.floor(m/2)].f then
temp = openlist[math.floor(m/2)]
openlist[math.floor(m/2)] = openlist[m]
openlist[m] = temp
whichlist[temp.x][temp.y] = m
m = math.floor(m/2)
whichlist[openlist[m].x][openlist[m].y] = m
else
break
end
end
tocheck[i] = nil
end
end
end
print("Checks: ", table.getn(tocheck))
for i, v in pairs(tocheck) do
print("Hi")
local tempG = myG + (math.abs(v[1]) ~= math.abs(v[2]) and 1 or 1.4)
listk = listk + 1
tempH = math.abs((curbase.x + v[1]) - tx) + math.abs((curbase.y + v[2]) - ty)
table.insert(openlist, listk, {x = curbase.x + v[1], y = curbase.y + v[2], g = tempG, h = tempH, f = tempG+tempH, par = closedk})
if not whichlist[openlist[listk].x] then
table.insert(whichlist, openlist[listk].x, {})
end
table.insert(whichlist[openlist[listk].x], openlist[listk].y, listk)
m = listk
while m ~= 1 do --<>
if openlist[m].f <= openlist[math.floor(m/2)].f then
temp = openlist[math.floor(m/2)]
openlist[math.floor(m/2)] = openlist[m]
openlist[m] = temp
whichlist[temp.x][temp.y] = m
m = math.floor(m/2)
whichlist[openlist[m].x][openlist[m].y] = m
else
break
end
end
end
end
print "huh"
return nil
end


function CalcPath(closedlist)

if closedlist == nil or table.getn(closedlist) == 1 then
return nil
end
local path = {}
local pathIndex = {}
local last = #closedlist
table.insert(pathIndex,1,last)

local i = 1
while pathIndex[i] > 1 do
i = i + 1
table.insert(pathIndex, i, closedlist[pathIndex[i - 1]].par)
end

for n = #pathIndex - 1, 1, -1 do
table.insert(path, {x = closedlist[pathIndex[n]].x, y = closedlist[pathIndex[n]].y})
end

closedlist = nil
return path
end
-- note: in the examplemap, y goes down from the top.
--example:
local examplemap = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1},
{1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,0,1,0,1,1,1},
{1,1,1,1,1,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,0,1,1,1,1},
{1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1},
{1,1,1,0,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1},
{1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1},
{1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1},
{0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1},
{0,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,0,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}
for i, v in pairs(CalcPath(CalcMoves(examplemap, 1, 1, 1, 25))) do print(v["x"], v["y"]) end
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
06 Jan 2013 11:18 PM
Bump
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
07 Jan 2013 03:03 AM
The behaviour of the next function's iteration compared to the order of definition is undefined.

Table={
index1=1,
index2=2,
index3=3,
index4=4,
index5=5
}
for i,v in next,Table do
print(i,v)
end

> index5 5
> index2 2
> index1 1
> index3 3
> index4 4
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
07 Jan 2013 03:05 AM
Fergot indentss :o

    local Table={
        index1=1,
        index2=2,
        index3=3,
        index4=4,
        index5=5
    }
    for i,v in next,Table do
        print(i,v)
    end

> index5 5
> index2 2
> index1 1
> index3 3
> index4 4
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
07 Jan 2013 06:10 AM
That's interesting, but why is the # operator and the getn function saying there is nothing in the table even though it's behaving like something is in it?
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
07 Jan 2013 08:14 AM
They use numeric indexes only.
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
07 Jan 2013 02:27 PM
My indexes are numeric, the values stored are arrays but I don't think that makes the difference. I'm going to try removing things from the tocheck array using table.remove instead of setting them to nil.
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
07 Jan 2013 02:35 PM
The weird thing is the error I'm getting when it doesn't run the loop

bad argument #1 to 'pairs' (table expected, got nil)

which is weird for two reasons:
1. the loop runs fine the first 70 or so times the program reaches it.
2. the print(#tocheck) line right before it uses the exact same table without any errors, if tocheck was nil, I should get an error on that line.
Report Abuse
lah30303 is not online. lah30303
Joined: 15 Feb 2008
Total Posts: 10027
07 Jan 2013 02:46 PM
Scratch my last post. The error I posted is on another line and is what happens when the loop doesn't run as expected. I was wondering why I didn't post it. Still the fact that the loop runs fine the first 70 times is really weird.
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
08 Jan 2013 06:47 PM
Like I said, the behaviour of the next function is undefined. `pairs{}` returns next, {}
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image