Incorrect Logic, Need Correct Logic

Hello.

I have created a script which would in theory randomly generate different chests depending on the number, but my logic seems to be incorrect. The code is as follows:

while true do
	print("Looped!!!!!!")
	local waittime = math.random(1,5)
	wait(waittime)
	if math.random(1,10) == 1 or 2 or 3 or 4 or 5 then
		print("Successfully created a Common Chest!")
	else
		if math.random(5,10) == 5 or 6 or 7 then
			print("Successfully created a Rare Chest!")
		else 
			if math.random(8,10) == 8 or 9 then
				print("Successfully created an Epic Chest!")
			else 
				print ("Successfully created a legendary chest!")
			end
		end
	end
end

Any help will be greatly appreciated.

Actually, I just solved it. Instead of using ==, I used <=, which would not require the or in it.

You are trying to use or on numbers to check the it contains the numbers.
Sorry in programming, it doesn’t work that way.
It’s truthy vs falsely, the or operator is left associative (at least in Lua and Python), so

print(1 or 2 or 3) --> 1
print(0 or 1 or 2) --> Lua: 0, Python: 1
print(false or 2) --> 2

== operator is calculated before the or operator so

print(1 == 2 or 3) --> 3

Please use “elseif” (or “elif” if you’re on Python or “else if” if you’re on C/C++C#) instead of “else” then “if”.

1 Like

If you would want to compare one value to several you have to re declare your original value and the one you want to compare it to.

if (a == 1) or (a == 2) or (a == 3) ...
1 Like

0 == 1 or 0 returns false
0 == 1 or 0 == 0 returns true

The syntax doesn’t allow checking 1 variable with multiple variables. But <= will solve ur solution because ur making the right comparison syntax.

Also the probability to get the legendary chest is smaller than 10%. Its 1/12.

1 Like

That’s ok. Just as long as its less than the chests previously it should be good. This currently is a PH, so I will balance it. Thanks for your help!

1 Like