Creating a luck based system that reads off a table

I’m currently trying to make a luck based system for catching fish in a game im making, i have a table that sets out all the values but when i go to run the function it does get to the for loop. my current code:

lucktable = {
	["Common"] = {1,70},
	["Uncommon"] = {71,90},
	["Rare"] = {91,97},
	["Legendary"] = {98,100}
}

function GoThroughLuckTable(Input)
	print("test")
	print(lucktable)
	for i, v in ipairs(lucktable) do
		if Input > lucktable[v][1] and Input  < lucktable[v][2] then
			print(lucktable[v])
		end
	end
end

GoThroughLuckTable(72)

Instead of going through the whole thing it only prints
image

1 Like

You’re using ipairs() when you should be using pairs().

If I remember correctly when I was doing this last time I did this I had to change it to pairs instead of ipairs. The reasoning for this is because ipairs is for a table such as {1, 2, 3, 4, 5, 6} while pairs is more for iterating through per say instances inside of the workspace. Each Item inside of your lucktable is defined as an Instance according to ROBLOX when it is reading it therefore when they go to continue with ipairs it hits a NIL value and stops the loop, ipairs will always stop when it reaches a Nil value while pairs should continue with only returning Nil

2 Likes
print(lucktable[v])

This will return the table with the boundaries of the number you gave, if you want the name, you can either add a name variable inside the table or make a function with another table to get it

There is no difference except the iPairs should be for things that cant be destoryed so it doesn’t return nil, pairs is better in both cases tho!

How would i add a name variable? i thought the ["____"] was the name no?

The difference is, pairs is used for table dictionaries (tables with keys). While ipairs is used for table arrays (tables with number indexes).

1 Like

The ["__"] is like an index for it not its actual name, to get the name, add a vraibale called name next to your luck boundaries / just a string at the end andake it like this

print(lucktable[v][3]) --third index will be the name after adding it!

That won’t affect how the loop runs tho…

When using pairs, your v variable will return the value of the key the for loop is currently at. So you would do:

for i, v in ipairs(lucktable) do
	if Input > v[1] and Input  < v[2] then
		print(v)
	end
end

Do this

lucktable = {
	["Common"] = {1,70,"Common"},
	["Uncommon"] = {71,90,"Uncommon"},
	["Rare"] = {91,97,"Rare"},
	["Legendary"] = {98,100,"Legendary"}
}

function GoThroughLuckTable(Input)
	print("test")
	print(lucktable)
	for i, v in ipairs(lucktable) do
		if Input > lucktable[v][1] and Input < lucktable[v][2] then
			print(lucktable[v][3])
		end
	end
end

GoThroughLuckTable(72)
1 Like

Another thing is, no need to do lucktable[v] just do v[1] and so on…

This clearly explains the difference between the two and how each should be used:
Topic: ipairs vs pairs

I do understand the difference clearly, thanks for that tho, learned the ordering thing.

1 Like
if Input > lucktable[v][1] and Input < lucktable[v][2] then
	print(i)
end

Just print the key directly.

it will print the key which will return a table and not the name which is what he wants