# Why is this printing out nil?

Hello, I was making a script to get a number within a certain range, but it returns nil

## the code

local function GetNumberInRange(Min, Max, ValueMin, ValueMax)

local function Get(Min, Max)
local Number = math.random(Min, Max)
return Number
end

local function Test()
local Numbers = Get(Min, Max)
if Numbers >= ValueMin and Numbers <= ValueMax then
return Numbers
elseif Numbers <= ValueMin and Numbers >= ValueMax then
Numbers = Get(Min, Max)
Test()
else
Numbers = Get(Min, Max)
Test()
end
end
Test()
end

local NewNumber = GetNumberInRange(1, 10,  2, 5)
print(NewNumber)

Firstly, the function seems useless as you can just do:

local NewNumber = math.random(2,5)

Also, itâ€™s inefficient to declare the â€śGetâ€ť and â€śTestâ€ť function in the function itself. However if you feel compelled to constrain the random number for whatever reason, you can use math.clamp:

local NewNumber = math.clamp(math.random(1,10), 2,5)
2 Likes

Oh, wow I did not know that it is that easy