|
| 05 Jun 2016 01:25 PM |
| How would I test if a value in a table does not exist without giving me an index error? |
|
|
| Report Abuse |
|
|
| |
|
|
| 05 Jun 2016 01:29 PM |
| I tried that in a bunch of different ways, but it always gives me "attempt to index '?' (a nil value)" |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 01:31 PM |
if tab[key] == nil then
end
?
|
|
|
| Report Abuse |
|
|
| |
|
|
| 05 Jun 2016 01:47 PM |
| What's the code ur using that's giving the error? |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 01:51 PM |
function addRoom() local x = math.random(1, 16) local y = math.random(1, 16) local width = math.random(5, 15) local height = math.random(5, 15) print(x, y, width, height) for i=1, width do for j=1, height do if not dungeon[x][y] then -- attempt to index '?' (a nil value) return end dungeon[x+i][y+j] = 1 end end end
=============================
I'm having a bunch of errors with this entire process but this is the only one that's completely breaking the script. |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 01:55 PM |
It's your logic.
dungeon[x] could be nil, wouldn't error. dungeon[x][y] would if the above case were true.
local Tab = {} print(Tab[1][1])
Throws the same error. |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 01:55 PM |
maybe this
for i,v in pairs(tab) do if v == val then return nil end end
|
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 01:55 PM |
That's ur problem Ur checking if [x][y] exists, but if [x] doesn't exist then ur basically doing nil[y] Which will give u an error
U have to check both, first [x], then [x][y] |
|
|
| Report Abuse |
|
|
fear2213
|
  |
| Joined: 20 Mar 2011 |
| Total Posts: 235 |
|
|
| 05 Jun 2016 01:56 PM |
function addRoom() local x = math.random(1, 16) local y = math.random(1, 16) local width = math.random(5, 15) local height = math.random(5, 15) print(x, y, width, height) for i=1, width do for j=1, height do if not dungeon[x] then return elseif not dungeon[x][y] then return end dungeon[x+i][y+j] = 1 end end end
Clans Admin > https://www.roblox.com/Clan-Admin-V1-6-Update-6-1-2016-item?id=419845359 < Clans Admin |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 01:57 PM |
U could combine both into one using an or ^ But ye, that |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 01:57 PM |
THANK YOU! :D
Btw, do any of you know some good articles on roguelike generation? I've tried a ton of stuff but I just can't wrap my head around the math and looping required for some of it |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 02:00 PM |
I would instead do
if not (dungeon[x] and dungeon[x][y]) then break
|
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 02:01 PM |
if it does an error if it doesn't exist, why not use pcall?
R$2,030 lol im poor |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 02:03 PM |
| Yeah I tried pcall but it gave me more problems so I ditched it |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 02:03 PM |
Because it's an avoidable error, that's just poor practice. Why patch the hole when you can fix it. |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2016 02:09 PM |
| I've tried this over a ton of times before and I got lazy this time. I just can't figure procedural generation out :( |
|
|
| Report Abuse |
|
|
|
| 09 Jun 2016 01:26 PM |
for the if statement use :FindFirstChild(), so it wont break
R$1,825 lol im poor |
|
|
| Report Abuse |
|
|