generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: How?

Previous Thread :: Next Thread 
LordDeofol is not online. LordDeofol
Joined: 29 May 2010
Total Posts: 614
08 Oct 2014 04:01 PM
I am trying to write a script that chooses a random word from a given selection, then changes the Text of a TextLabel to that. I am unsure how to go about to do this. For now, this is all I have:

while true do
script.Parent.WeatherDistrict.Text = "District One"
script.Parent.WeatherType.Text
script.Parent.WeatherDegrees.Text

--not posting rest of script--


Essentially, this is a weather script. I need the WeatherType text that the script selects randomly from, to be: "Windy", "Foggy", "Sunny", "Rainy", "Snowy", or "Cloudy"

Please help.

Report Abuse
LordDeofol is not online. LordDeofol
Joined: 29 May 2010
Total Posts: 614
08 Oct 2014 04:19 PM
I have gotten it, however, where would I add a wait() so the text changes to another random pick?

math.random(1, 2, 3, 4, 5, 6)
if math.random == 1 then
script.Parent.WeatherType.Text = "Sunny"
end

if math.random == 2 then
script.Parent.WeatherType.Text = "Cloudy"
end

if math.random == 3 then
script.Parent.WeatherType.Text = "Rainy"
end

if math.random == 4 then
script.Parent.WeatherType.Text = "Foggy"
end

if math.random == 5 then
script.Parent.WeatherType.Text = "Snowy"
end

if math.random == 6 then
script.Parent.WeatherType.Text = "Windy"
end


end
Report Abuse
LordDeofol is not online. LordDeofol
Joined: 29 May 2010
Total Posts: 614
08 Oct 2014 04:19 PM
My apologies, add an "while true do" to the beginning of the script above.
Report Abuse
purple12jason12 is not online. purple12jason12
Joined: 31 Aug 2010
Total Posts: 72
08 Oct 2014 04:22 PM
The simplest way to randomize something is to use the math.random function. Basically, what that does is selects a random number between the first and last numeral inputs. For example, you have 6 different weather types. If you wrote a function such as this;
"function Randomize()
Number = math.random(1,6)
end"

You would get a value between 1, and 6.

How to use a random number to identify a string?

There are two ways. The longest and easiest is to do something like;
"if Number == 1 then
something = something else
end"

But the best and most efficient way is to make a table with the 6 strings on it, then you'd do this;
"Text = (TableName[Number})"

This line sets Text to the random member of the table.

If this is confusing, I suggest Wiki.


Report Abuse
LordDeofol is not online. LordDeofol
Joined: 29 May 2010
Total Posts: 614
08 Oct 2014 04:34 PM
Yes, I just created that. However, it isn't working. May you check it? Here it is:

while true do
wait(5)
math.random(1,6)
if math.random == 1 then
script.Parent.WeatherType.Text = "Sunny"
end

if math.random == 2 then
script.Parent.WeatherType.Text = "Cloudy"
end

if math.random == 3 then
script.Parent.WeatherType.Text = "Rainy"
end

if math.random == 4 then
script.Parent.WeatherType.Text = "Foggy"
end

if math.random == 5 then
script.Parent.WeatherType.Text = "Snowy"
end

if math.random == 6 then
script.Parent.WeatherType.Text = "Windy"

wait(1)
end
end
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
08 Oct 2014 04:39 PM
Let's take a look at tables.
Tables hold multiple values inside them and can be interated (read thru by the program) to do things with the values.

Here is an example of a table:
local example = {1, 2, "a", "b", false, true}

How we retrieve elements from a table is like this:
example[1], example[2], etc.

Here is an example of table manipulation:

local example = {1, 2, 3}

local exampleFunc = function(tab)
local out = {}
for i,v in pairs(tab) do
out[i] = v * 2
end
return out
end

local newTable = exampleFunc(example)

Here is the description of this...
First we create a table. Then I created a function which required a table to be passed thru. It would then iterate over the contents and make a new table with each value multiplied by two.

Here is the contents of newTable...
local newTable = {2, 4, 6}

..........................

Using math.random, it requests a range.

Try: math.random(1,6), for it will give you a number from 1 to 6.


Report Abuse
128GB is not online. 128GB
Joined: 17 Apr 2014
Total Posts: 8056
08 Oct 2014 04:41 PM
local weather = {
"Sunny";
"Cloud";
"Rainy";
"Foggy";
"Snowy";
"Windy";
}

while wait(6) do
script.Parent.WeatherType.Text = weather[math.random(#weather)]
end
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image