Nil value from dictionary?

Heyo,

I’m trying to make a rock spawning system that gets the rock’s properties from a dictionary but am encountering issues with a nil value.
Whenever the ‘rockType’ is defined at the start, the system somewhat works (but only spawns that rock type), but when I leave it empty, the ‘rockTypes[rockType].size’ becomes nil. It’s like it’s avoiding the if-statement that defines the ‘rockType’ right after.

(‘rockTypes’ is the dictionary, ‘rockType’ is a string value defining each element within)
Any idea what I’m missing? (screenshots below)

1 Like


I think you are using an empty variable in here.
You can use something like this

local rockSize = rockTypes.SandRock.size
local rockSize = rockTypes.Salt.size

Let me know if it helps.

If spawnType is not 1, the code will error because rockType will be nil. Should you be accounting for different spawn types?

1 Like

Roblox doesn’t make logic errors. In the second screenshot where you leave rockType as undefined, its value is determined by the spawnType and random variables. Your issue here is that spawnType is simply not equal to 1.

You should add a print inside the if spawnType == 1 then code block to confirm that the if statement is being passed. And add more prints in the nested if and else statement code blocks.

The variable should be defined in the if-statement just after

I printed the spawnType and it’s in fact 1. It’s a value that’s stored as attribute in the rock model itself, currently there only is 1 but I plan to add more later.

This is super weird, I don’t suppose lua differentiates in number and string values right?
image

EDIT, it was in fact a string value

If spawnType is a string, then it won’t work. It has to be a number.

1 Like

Okay that was so stupid, sorry for bothering and thanks for the help!

Since 1 and "1" are different types, there’s no way they are equal.

Some coding languages with less restrictions like JavaScript do allow this type of comparison being true, which is why they most likely also have a === comparer which checks if they’re the same value AND type. This is more confusing that Lua, though.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.