|
| 06 Aug 2012 05:47 PM |
I am building a script that has an array for random parts, and picks a random part in the array. Here is what i have so far:
function findNearestHarbor() local Harbors = {Workspace.Harbors.Harbor1,Workspace.Harbors.Harbor2,Workspace.Harbors.Harbor3}
If you could help that would be great. Thanks
|
|
|
| Report Abuse |
|
|
Xnite515
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 22763 |
|
|
| 06 Aug 2012 05:49 PM |
math.random(harbors)
I think that;s right.. |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
| |
|
|
| 06 Aug 2012 05:55 PM |
so in this case i would be Math.random(1,17)?? were does it pick from the array? |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 06 Aug 2012 05:56 PM |
| Is that all of the script you have? If you're trying to do what I think you're doing, then I think there might be a more efficient way. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 07:01 PM |
here is all i have:
function findNearestHarbor() math.randomseed(tick()) local Harbors = {Workspace.Harbors.Harbor1,Workspace.Harbors.Harbor2,Workspace.Harbors.Harbor3,Workspace.Harbors.Harbor4,Workspace.Harbors.Harbor5,Workspace.Harbors.Harbor6,Workspace.Harbors.Harbor7,Workspace.Harbors.Harbor8,Workspace.Harbors.Harbor9,Workspace.Harbors.Harbor10,Workspace.Harbors.Harbor11,Workspace.Harbors.Harbor12,Workspace.Harbors.Harbor13,Workspace.Harbors.Harbor14,Workspace.Harbors.Harbor15,Workspace.Harbors.Harbor16,Workspace.Harbors.Harbor17} RandomBrick = Math.random(Harbors)
|
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 06 Aug 2012 07:02 PM |
Oh my o.o Use this instead: local randomBrick = workspace.Harbor["Harbor"..tostring(math.random(1, 17))]; |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 07:04 PM |
if you notice, my workspace is:
Workspace.Harbors.Harbor#
so how would i incorperate that into a script |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 06 Aug 2012 07:06 PM |
The way I just should you. The ".." is string concatentation. "Harbor"..tostring(math.random(1, 17)) would generate a random Harbor# between 1 and 17, and as all your parts are called Harbor#, it would be fine. |
|
|
| Report Abuse |
|
|
| |
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 06 Aug 2012 07:09 PM |
Yw :3 This is a nice technique to use when all yours objects are named similarly like that. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 07:10 PM |
well on thing, my workspace has is like:
Workspace Harbors Harbor1 Harbor2 ... .. . ....
..
.. .... Harbor16 Harbor17
would it still work? |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 06 Aug 2012 07:11 PM |
| Yeah I believe so. The best way to find out is to try it yourself! :3 |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 07:17 PM |
AWESOME!!! I ran this:\ function findNearestHarbor() math.randomseed(tick()) local randomBrick = Workspace.Harbor["Harbor"..tostring(math.random(1, 17))]; print(Harbor[3]:GetFullName()) end
on output and it worked!! thx so much :) |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
| |
|