Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get the random number from a function?

Asked by 3 years ago
Edited 3 years ago
script.Parent.Triggered:Connect(function ()
    local function rng()
        return math.random(1, 10)
    end
    if rng() >= 3 then
        print ("Number:"..rng().." Success")
    else
        print ("Number:"..rng().." Fail")
    end
end)

I was just messing around with having a part with a ProximityPrompt in it and a script which you see above. I'm trying to get the local function rng() to print to show exactly what number was produced in relation to it succeeding (by being 3 or over) or failing (being 2 or less). I realize that I can't call the local function to print the number because that'll just produce a whole new random number between 1 and 10 and have no relation to the if statement criteria.

So, my question is how would I go about printing the initial number from the local function rng() to show in relation to if the number succeeded or failed in the if statement? Any help is appreciated. Thank you!

0
Create a variable after the function and assign it to 'rng()', this way you don't need to keep calling the function and generating a new random number, the number that was just returned will be assigned to the variable. xInfinityBear 1777 — 3y
0
That solved it, I must've been putting the variable in the wrong place or had tried the variable thing when I had the function outside of the proximityprompt function so it was just showing the initially generated number. Solved! adashofSiren 0 — 3y

Answer this question