How to get a name from a array?

Started by Debian, Sep 12, 2015, 03:22 PM

Previous topic - Next topic

Debian

Nogoto + on works perfect, but there in nogoto off i get a problem at  nogoto.remove( player.Name ); because it is not a integer. I dont want to use player.ID because  i will save that array while unloading. So this can prevent additional database from making and reduce usage of a lot of databases because i will be using for other scripts too.

else if ( cmd == "nogoto" ) {
if (params[0] == "on") {
if(nogoto.find( player.Name ) == null){
nogoto.push( player.Name );
MessagePlayer( "Players cannot teleport to you.", player );
}
else MessagePlayer( "You have already enabled nogoto.", player );
}
else if (params[0] == "off") {
if(nogoto.find( player.Name ) != null){
nogoto.remove( player.Name );
MessagePlayer( "Players can teleport to you.", player );
}
else MessagePlayer( "You have already disabled nogoto.", player );
}
else  MessagePlayer( "Correct Syntax : /" + cmd + " on/off ", player );
}

Thijn

Use tables.

local nogoto = {};
nogoto.rawset(player.Name, true);
nogoto.rawget(player.Name);
nogoto.delete(player.Name);

DizzasTeR

According to what I get from your fair enough English, try this.

nogoto.remove( nogoto.find( player.Name ) != null ) );

Thijn

Quote from: Doom_Killer on Sep 12, 2015, 03:38 PMAccording to what I get from your fair enough English, try this.

nogoto.remove( nogoto.find( player.Name ) != null ) );
Did you test that? :P
(Hint: The answer is probably no)

.

#4
Quote from: Doom_Killer on Sep 12, 2015, 03:38 PMnogoto.remove( nogoto.find( player.Name ) != null ) );

What @Thijn meant:

... ) );
Leftover bracket!

... nogoto.find( player.Name ) != null ...
Wrong use of an expression. The result is always one of two possibilities. Either remove whatever exists at the index "true" if player exists or the index "false" if it doesn't.

But this is an array (if I understand correctly) and thus the boolean result from the expression might be casted to an integer to satisfy the argument requirements and you're again left two possible results. Remove what's at index 0 if the expression is "false" or remove what's at index 1 if the expression is "true".

Final answer: DON'T!
.

DizzasTeR

Don't expect me to be dashing for everyone, just open your eyes and you will find some useful words, IF and only IF you get the point. ( hint: idea ) :p

FinchDon

Why you want in array use simple one :\
For any help and support Join #s-s at IRC for Help in Scripting
( For Newbies )

KAKAN

Why ya use like that?
NOTE:- This is just a suggestion.
This one is way far easier than the one u made:-
function onScriptLoad()
{
nogoto <- array(GetMaxPlayers(), false);
}

Here is the cmd:-
else if ( cmd == "nogoto" )
{
if(!text)PrivMessage(player,"Use "+cmd+" <on/off>.");
else
{
switch(text.tolower())
{
case "on":
if ( nogoto[ player.ID ] ) MessagePlayer(">> Your nogoto is already on.",player);
else
{
nogoto[ player.ID ] = true;
PrivMessage(player,"Your nogoto is now on.");
}
break;

case "off":
if ( !nogoto[ player.ID ] ) MessagePlayer(">> Your nogoto is already off.",player);
else
{
nogoto[ player.ID ] = false;
PrivMessage(player,"Your nogoto is now off.");
}
break;

default: PrivMessage(player,"Syntax Error: Use /" + cmd + " <on/off>."); break;
}
}
}

Now you know what is needed to add on your goto cmd tho, Good Luck!
oh no

Debian

Kakan and finch, this one is for strong in database/... when server unloads.

Finally made it, does  rawset will create a new value each time it is called?

nogoto <- {};   

Onplayerjoin :
   nogoto.rawset(player.Name, true);

else if ( cmd == "nogoto" ) {
if (params[0] == "on") {
if( nogoto.rawget( player.Name ) == false){
nogoto.rawset(player.Name, true);
MessagePlayer( "Players cannot teleport to you.", player );
}
else MessagePlayer( "You have already enabled nogoto.", player );
}
else if (params[0] == "off") {
if( nogoto.rawget( player.Name ) == true){
nogoto.rawset(player.Name, false);
MessagePlayer( "Players can teleport to you.", player );
}
else MessagePlayer( "You have already disabled nogoto.", player );
}
else  MessagePlayer( "Correct Syntax : /" + cmd + " on/off ", player );
}


Thijn

Quote from: Debian on Sep 12, 2015, 05:14 PMFinally made it, does  rawset will create a new value each time it is called?
It will not if you use the same key.

Debian

This line    nogoto.rawset(player.Name, false); in onplayerjoin is making it false each time a player joins and if i dont use it always gives errors as index of player.Name doesn't exist. for rawget(player.Name).  I tried    nogoto.rawin(player.Name); in onplayerspawn and if( nogoto.rawget( player.Name ) == null ) in command to test whether it works but it is still not working.

Thijn

Post your code and a screenshot of the error message.