Local part = script.parent
part.Touched:connect(Function(hit)
local number = math.random(1,10)
If number > 2 then
Print(“greater than 2)”
If number > 3 then
Print(“Greater than 3)”
End
End
End)
Let’s say the Number was 9.
Both of the print functions would print.
The thing is I only want it to print if it’s higher than the number before
(So if you got a 4, it’d not print the 2 but the 3 because there is no higher number to do)
I want to know this to make more complex things with like 1/5000 chance with different stuff in between without getting them all
Firstly Function F is small letter not big so write “function” the same thing with If its “if” and dont write print like this print(“hi)” this causes an error write it print(“hi”)
There are several beginner mistakes in your script, including the capitalization of globals (“local”, “print”)
Also, it’d be a lot of help to the people reading this post if you formatted your script.
Anyways, is this what you were looking for? It’s untested but I’m confident that it’ll work.
local previousNumber = 0
script.Parent.Touched:Connect(function(hit)
if not hit.Parent then return end
if not hit.Parent:FindFirstChildWhichIsA("Humanoid") then return end
local randomNumber = math.random(1, 10)
if randomNumber >= previousNumber then
print(string.format("Random Number (%s) is greater than the Previous Number (%s)",
randomNumber, previousNumber)
)
end
previousNumber = randomNumber
end)
Might want to add a cooldown for it too if you wish.
local tableOfNumbersInDescOrder = {"8", "7", "5", "4", "2"}
local part = script.Parent
local function onPartTouched(hit)
local aRandomNumber = math.random(1, 10)
for _, numberFromTable in ipairs(tableOfNumbersInDescOrder) do
if aRandomNumber < numberFromTable then
print(aRandomNumber.. " is less than "..numberFromTable..".")
break
end
end
end
Use a table of numbers in descending order (in order of largest to smallest) as to avoid an excessively long chain of conditional statements.
As the above post suggested, you may want to add a cooldown (to prevent the function from executing too frequently).