Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

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())

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.