Using Math Random without it being a Variable

Simply put, when a player completes an action, I want the script to do a math.Random function to give them a random object. The reason why I don’t what it to be a variable is because, from what I understand, once a math.Random variable is made, it will continue using the number that was originally picked throughout the entire script. So, just wondering if this is possible, and if so, how?

You can still use math.random() as a variable in a smaller scope. Every function has a “Scope” where any variable created inside that scope will stop existing outside the function. The issue you are facing is declaring your random variable in the global scope (say at the top of your script), this will only get one random value for the entire global scope.

local global_random = math.random()

local function random_scoped()
    local function_scope_random = math.random()
    print(function_scope_random)
end

print(global_random)
print(global_random)
print(global_random)
-- ^^ all the same

random_scoped()
random_scoped()
random_scoped()
-- ^^ three different numbers

TLDR: The take-away for this example is to declare you random inside the function you use to give random objects. If you post your function here I can show you how to apply it specifically.

1 Like

So as long as the math.Random is in a function, it will be rerolled each time the function is called?

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.