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

How do I make a math.random script activate upon the click of a TextButton?

Asked by 4 years ago

I am trying to make a math.random script execute when I press a TextButton, how can I do this? I've attempted to make it execute through a function but have had little success. I am very new to Roblox scripting, so I'm probably just doing something wrong.

math.randomseed()
    for _ = 1, 10 do
    print(math.random(20))
    wait(3)
end

For the function, I tried:

function Roll
()for _ = 1, 10 do
    print(math.random(20))
    wait(3)
    end
end

Additionally, what's a way I can I make it show up on a BillboardGui above the player's head?

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Simple

****1. This is assuming the script is parented to the Image/Text Button

if script.Parent.MouseButton1Click:Connect() then
    local generated = math.random(Input1,Input2)
    print(generated) -- for checking if it ran properly for testing
    -- Continue script as necesarry
end
Ad
Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

For your math.random, you need to put in another argument. An example:

print(math.random(1,5))

To make it print a random number between two numbers, you must have the beginning number (1 in this case) and the ending number (5 in this case). If you don't include either of them, it will automatically generate a random number between 0 and 1 (will usually come out as a wacky decimal, whereas including both numbers will make it always come out a whole number.)

An example of no arguments for math.random:

print(math.random())
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

its quite simple, all you have to do is listen for the MouseButton1Down event of TextButtons..

for instance - assuming the script is parented to the textbutton:

script.Parent.MouseButton1Down:Connect(function()
    local randomNum = math.random(1, 20);
    print("generated random number =", randomNum)
end)

Answer this question