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 » Scripters
Home Search
 

Re: JavaScript help

Previous Thread :: Next Thread 
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
22 Nov 2014 11:07 AM
I would be posting this in SH, but nobody there knows java script.


anyways, this is supposed to choose a random item from a table based off of its weight (the number).


I'm currently getting the error

"assignment to undeclared variable Miner"

btw the math.RandomInt is a function from a module script outside. It works.

Sorry if you see this as spam, I just started Tuesday and need some help.

var math = require("MathHandler");
// math.random example V
//console.log(math.RandomInt(1,5));


var chance = [

Miner = 20, "Miner",
Defender = 15, "Miner",
Healer = 15, "Miner",
Outer_Defender = 7,"Miner",
Builder = 13, "Miner",
Repairman = 10, "Miner",
Raider = 20 ,"Miner",

];

var NewChance = [];
var counter = 0;
for (var i = 0; i < chance.length+1; i++) {

for (var k = 0; i == chance[i][0]; k++){

NewChance[counter] = chance[i];
counter=counter+1;

}
}


function select() {

return NewChance[math.RandomInt(1,NewChance.length)][1];


}

console.log(select());


Report Abuse
digpoe is not online. digpoe
Joined: 02 Nov 2008
Total Posts: 9092
22 Nov 2014 11:08 AM
you would be posting this in a non-existant forum but nobody in the non-existant forum knows JS?

gg
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
22 Nov 2014 11:09 AM
oh yeah. Whoops. haven't been online in a while.


I would post this in GD, but..


yeah you know.
Report Abuse
digpoe is not online. digpoe
Joined: 02 Nov 2008
Total Posts: 9092
22 Nov 2014 11:09 AM
:p
Report Abuse
Argelius is not online. Argelius
Joined: 19 Jul 2010
Total Posts: 4047
22 Nov 2014 11:11 AM
night omg that was close

you cant let anyone know that sh is hidden now
Report Abuse
anaIyze is not online. anaIyze
Joined: 29 May 2014
Total Posts: 2048
22 Nov 2014 11:15 AM
'"Miner",'

why
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
22 Nov 2014 11:16 AM
Its an RTS game where you have to program the system to make your units defend/attack/whatever.

Called screeps.

Report Abuse
anaIyze is not online. anaIyze
Joined: 29 May 2014
Total Posts: 2048
22 Nov 2014 11:18 AM
well when you put just "Miner" in the table of course its going to error with nil val/var
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
22 Nov 2014 11:19 AM
crap. Your right.


I must be tired.
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
22 Nov 2014 11:25 AM
alright, this is my table now but it still is throwing the same error as before.

var chance = [

Miner = [20, "Miner"],
Defender = [15, "Defender"],
Healer = [15, "Healer"],
Outer_Defender = [7,"Outer_Defender"],
Builder = [13, "Builder"],
Repairman = [10, "Repairman"],
Raider = [20 ,"Raider"],

];
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
22 Nov 2014 11:36 AM
B#1
Report Abuse
BothAngles is not online. BothAngles
Joined: 01 Dec 2011
Total Posts: 9604
22 Nov 2014 11:42 AM
var chance = [];
chance["Miner"] = [20, "Miner"];
chance["Defender"] = [15, "Miner"];
chance["Healer"] = [15, "Miner"];
chance["Outer_Defender"] = [7,"Miner"];
chance["Builder"] = [13, "Miner"];
chance["Repairman"] = [10, "Miner"];
chance["Raider"] = [20 ,"Miner"];

console.log(chance["Miner"]);
-- [20, "Miner"]
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
22 Nov 2014 11:59 AM
Thanks! I just had to fix up a few things, but I ran into another problem.

error = "NewChance[math.RandomInt(...)] is undefined"


var math = require("MathHandler");
// math.random example V
//console.log(math.RandomInt(1,5));


var chance = [];
chance[0] = [20, "Miner"];
chance[1] = [15, "Miner"];
chance[2] = [15, "Miner"];
chance[3] = [7,"Miner"];
chance[4] = [13, "Miner"];
chance[5] = [10, "Miner"];
chance[6] = [20 ,"Miner"];

var NewChance = [];
var counter = 0;
for (var i = 0; i == chance.length; i++) {

for (var k = 0; i == chance[i][0]; k++){

NewChance[counter] = chance[k];
counter=counter+1;

}
}


function select() {

return NewChance[math.RandomInt(1,NewChance.length)][1];


}

console.log(select());




Report Abuse
BothAngles is not online. BothAngles
Joined: 01 Dec 2011
Total Posts: 9604
22 Nov 2014 12:20 PM
var chance = [
[20, "Miner"]
, [15, "Miner"]
, [15, "Miner"]
, [7,"Miner"]
, [13, "Miner"]
, [10, "Miner"]
, [20 ,"Miner"]
];

var NewChance = [];
var counter = 0;
for (var i = 0; i < chance.length; i++) {
for (var k = 0; k < chance[i][0]; k++) {
NewChance[counter] = chance[i];
++counter;
}
}

// Returns a random integer between min (included) and max (excluded)
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}

function select() {
return NewChance[getRandomInt(1,NewChance.length)][1];
}

console.log(select());
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
22 Nov 2014 01:57 PM
Thanks!
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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