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

Math.random on typewriter script?

I have been playing around with a typewriter script to make many different other things, but when it comes to using math.random in them, it messes up the way it works.

In this script, when math.random is applied, it will only print the first few characters and then stop completely without giving any errors. This is currently running from a local script as it is in a gui which filtering enabled automatically clones to the player.

The script is:

local Choices= {"Choice 1","Choice 2"}
for i = 1, #Choices do
script.Parent.frame.Title.Text = string.sub("We have picked: "..text[math.random(1,#Choices)],1,i)
				wait()
		end

Thanks.

2 answers

✓ Accepted answer

###This is because your loop is looping until the length of the table, not the length of the string. You'll want to get the random string and define as a variable first, then loop until its length with the len function.

local choices = {"We have picked: Choice 1", "We have picked: Choice 2"}

local choice = choices[Random.new():NextInteger(1, #choices)]

for i = 1, choice:len() do -- to the length of the string
    script.Parent.frame.Title.Text = choice:sub(1, i)
    wait()
end

4 comments

0

Thank you, this worked very well.

0

Fact: You should be using the new data type Random; To generate random integers, you do Random.new():NextInteger(min,max). To generate random decimals, you do Random.new():NextNumber(min,max). Using Random is much faster (as it uses a different algorithm) than math.random()

0

Ah okay, I didn't really understand Random.new, I did now with this comment. Thank you.

0

Your welcome. (By the way you can do local seed = Random.new(tick()) like math.randomseed(tick()) then use seed:NextInteger() or seed:NextNumber() without typing in Random.new() a lot of times)

local Choices= {"Choice 1", "Choice 2"}
for I = 1, #Choices do
script.Parent.frame.Title.Text = string.sub("We have picked: " .. Choices[math.random(1, #Choices)], 1, I)
	wait()
end

I am confused as to what you are trying to accomplish, but try this.

1 comment

0

Thank you, I will try this out.

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