Author Topic: Incorrect script, need fixing  (Read 1573 times)

Hi, before you say anything this is for a friend, he trys to run a script, but it wont work on his server, It is a script for mini empires, The script is:
 function serverCmdRollDice(%client)
{
%r = getRandom(1, 5);
if(%r % 2)
messageall('', "\c3" @ %client.getplayername() @ "\c6 rolled \c3" @ %r @ "\c6! Hit!");
else
messageall('', "\c3" @ %client.getplayername() @ "\c6 rolled \c3" @ %r @ "\c6! Miss!");
}



He siad ottosparks gave it to him.
« Last Edit: December 30, 2011, 12:54:53 PM by BBOY »

try if(%r % 2 == 0) instead of if(%r % 2)
edit: instead of %client.getplayername() try %client.name

Nothing is wrong with the code, its packaged incorrectly or not enabled or something similar.

function serverCmdRollDice(%client)
{
%r = getRandom(1, 5);
if(%r == 2)
messageall('', "\c3" @ %client.name() @ "\c6 rolled \c3" @ %r @ "\c6! Hit!");
else
messageall('', "\c3" @ %client.name() @ "\c6 rolled \c3" @ %r @ "\c6! Miss!");
}

Please stop trying to fix it.

Nothing is wrong with the code, its packaged incorrectly or not enabled or something similar.

In fact, adam savage's code wouldn't even work.

Nothing is wrong with the code, its packaged incorrectly or not enabled or something similar.
You do not need to package new functions, only when overwriting existing ones.



Code: [Select]
function serverCmdRollDice(%client)
{
%rand = getRandom(1, 6);

switch(%rand)
{
case 6:
case 5:
case 4:
case 3:
case 2: messageClient(%client, '', "\c6You rolled a 2! LOL!"); %client.player.kill();
case 1:
default: messageClient(%client, '', "\c6What kind of die is that?");
}
}

getRandom will generate a number between 1 and 6. I don't know where you got 1 through 5 from.

switch() allows you to handle one variable based on many different values easily.

default: handles all exceptions not covered by specific cases. Since 1-6 is cased in the above code, the default message is entirely unnecessary -- hence the message.


The percent sign (%) is called the modulus sign. It gets the remainder of division by the following number. It's like division in elementary school, where the leftovers were put on the side as "r5" instead of being a decimal place.

Code: [Select]
function serverCmdTest(%client)
{
%rand = getRandom(0, 9);

if( (%rand % 3 == 0) && (%rand != 0) )
{
messageClient(%client, '', "You rolled a" SPC %rand @ ", which is evenly divisible by 3.");
}
}

You do not need to package new functions, only when overwriting existing ones.

He meant when you throw your stuff into a Server_zipFile and put it in the add-ons folder.

Welcome back Iban.  :cookieMonster:

Do people seriously develop Add-Ons in .ZIP format?

getRandom will generate a number between 1 and 6. I don't know where you got 1 through 5 from.
No, it will generate a number between whatever you tell it to.

Do people seriously develop Add-Ons in .ZIP format?

I don't, he may though..

You do not need to package new functions, only when overwriting existing ones.
Not what he meant at all.

Not what he meant at all.
He meant when you throw your stuff into a Server_zipFile and put it in the add-ons folder.

Welcome back Iban.  :cookieMonster:

No, it will generate a number between whatever you tell it to.
I hope you're just kidding.

Not what he meant at all.
I see that now. I didn't think about the OP being unable to put a script file in a folder when I wrote that post.

Code: [Select]
function serverCmdRollDice(%client)
{
%rand = getRandom(1, 6);

switch(%rand)
{
case 6:
case 5:
case 4:
case 3:
case 2: messageClient(%client, '', "\c6You rolled a 2! LOL!"); %client.player.kill();
case 1:
default: messageClient(%client, '', "\c6What kind of die is that?");
}
}
It's not good practice to have blank cases, just delete the blank cases and have the default, it's like a else.
Replace all the blank cases with the default, like this:
Code: [Select]
function serverCmdRollDice(%client)
{
%rand = getRandom(1, 6);

switch(%rand)
{
case 2:
messageClient(%client, '', "\c6You rolled a 2! LOL!"); %client.player.kill();
default:
messageClient(%client, '', "\c6What kind of die is that?");
}
}
Package that with a description and drop the sucker into add-ons.

Except rolling anything but a 2 will render the default message. The objective was to show him how to do a switch to handle specific rolls.

If he was testing for just a 2, all you have to do is if(%rand == 2)