Attempt to compare nil < number

This is supposed to be a simple card picker. There is a local script running which does animations and everything for a crate and then through a remote event tells the server which crate was bought. The server takes this then randomly gives a card which will eventually be given back to the client through a remote event. Only problem is this so far.

When comparing the number to the number in the table it returns nil. I have tried doing to number and to string. In the output, it’s getting the things it needs just can’t put them together:
Screenshot (10)

I have looked around and from what I saw, no one has had the same issue as me.

local event = game.ReplicatedStorage.Crates.Cards.Rarity

local Common = {0, 0, 0, 0, 0, --[[]] 100, 100, 0, 0, 0, 0, 0,
	0.1, 99, 0.8, 0.199, 0.001, --[[]] 99.9, 90, 7, 2, 0.8, 0.199, 0.001,
	5, 99, 0.8, 0.199, 0.001, --[[]] 95, 79, 15, 5, 0.8, 0.199, 0.01}

event.OnServerEvent:Connect(function(player, rarity)
	print(player)
	print(rarity)
	local card1type = math.random(1, 100000) / 1000
	print(card1type)
	print(tostring(rarity)[1])
	if card1type > (tostring(rarity)[1]) then
		print("Extra")
		local card1rarity = math.random(1, 100000) / 1000
		if card1rarity < rarity[12] then
			print("Special")
		elseif card1rarity < rarity[11] then
			print("Legendary")
		elseif card1rarity < rarity[10] then
			print("Epic")
		elseif card1rarity < rarity[9] then
			print("Rare")
		elseif card1rarity < rarity[8] then
			print("Uncommon")
		elseif card1rarity < rarity[7] then
			print("Common")
		end
	else
		print("Person")
	end
end)

In which line is it giving the error?

if card1type > (tostring(rarity)[1]) then

When you printed out the card1Type, did it print out nil?

Yes. In the screenshot, the first line is the

print(player)

The second line is the

print(rarity)

The third line is the

print(card1type)

And the fourth is the

print(tostring(rarity)[1])

I know but I wanted to know what prints out when you print the card1Type

Printing card1type prints a number with three decimal places.
printing (rarity[1]) prints nil.
That is the problem

Try this:

event.OnServerEvent:Connect(function(player, rarity) --Pass the rarity as a string
    local SelectedRarity = nil
    if rarity == "Common" then
       SelectedRarity  = Common
    end
	print(player)
	print(rarity)
	local card1type = math.random(1, 100000) / 1000
	print(card1type)
	print(SelectedRarity[1])
	if card1type > (SelectedRarity[1]) then
2 Likes

That seems such a simple fix. I should have thought of it. Thank you

2 Likes