|
| 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 |
|
|
|
| 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 |
|
|
|
| 08 Oct 2014 04:19 PM |
| My apologies, add an "while true do" to the beginning of the script above. |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|