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 » Scripters
Home Search
 

Re: How to know if a table value does not exist?

Previous Thread :: Next Thread 
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
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
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
05 Jun 2016 01:26 PM
if not Tab[StrKey] then
Report Abuse
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
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
Immersible is not online. Immersible
Joined: 14 Aug 2015
Total Posts: 432
05 Jun 2016 01:31 PM
if tab[key] == nil then

end

?


Report Abuse
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
05 Jun 2016 01:33 PM
Yep :(
Report Abuse
Wayne_Gretzky is not online. Wayne_Gretzky
Joined: 03 Apr 2008
Total Posts: 78011
05 Jun 2016 01:47 PM
What's the code ur using that's giving the error?
Report Abuse
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
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
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
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
Lord_Narwhal is not online. Lord_Narwhal
Joined: 05 May 2012
Total Posts: 3242
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
Wayne_Gretzky is not online. Wayne_Gretzky
Joined: 03 Apr 2008
Total Posts: 78011
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 is not online. 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
Wayne_Gretzky is not online. Wayne_Gretzky
Joined: 03 Apr 2008
Total Posts: 78011
05 Jun 2016 01:57 PM
U could combine both into one using an or ^
But ye, that
Report Abuse
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
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
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
05 Jun 2016 02:00 PM
I would instead do

if not (dungeon[x] and dungeon[x][y]) then
break
Report Abuse
ExpensiveGaming is not online. ExpensiveGaming
Joined: 01 Apr 2013
Total Posts: 783
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
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
05 Jun 2016 02:03 PM
Yeah I tried pcall but it gave me more problems so I ditched it
Report Abuse
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
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
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
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
ExpensiveGaming is not online. ExpensiveGaming
Joined: 01 Apr 2013
Total Posts: 783
09 Jun 2016 01:26 PM
for the if statement use :FindFirstChild(), so it wont break


R$1,825 lol im poor
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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