/slap cmd Gettok arguments

Started by umar4911, Oct 22, 2017, 04:38 AM

Previous topic - Next topic

umar4911

I have made many commands in my server. The problem I am facing is with getTok.
See this as an example
else if(cmd == "slap")
{
if(status[player.ID].Level < 3) MessagePlayer("[#FFDD33]Information:[#FFFFFF] Unauthorized Access", player);
else if(!arguments) MessagePlayer("[#FFDD33]Command Error:[#FFFFFF] Use /" + cmd + " [plr] [Reason]", player);
else if(NumTok(arguments, " ") < 2) MessagePlayer("[#FFDD33]Command Error:[#FFFFFF] Use /" + cmd + " [plr] [Reason]", player);
else
{
local plr = FindPlayer(GetTok(arguments, " ", 1));
if(!plr) MessagePlayer("[#FFDD33]Usage Error:[#FFFFFF] Invalid Player", player);
else if(status[player.ID].Level < status[plr.ID].Level) MessagePlayer("[#FFDD33]Usage Error:[#FFFFFF] You cannot give any admin higher than you", player);
else
{
plr.Pos = Vector(plr.Pos.x, plr.Pos.y, plr.Pos.z + 5);
local reas = GetTok(arguments, " ", 2);
Message("[#FFDD33]Administrator Command:[#FFFFFF] Admin[" + player.Name + "] slapped player : " + plr.Name + " Reason : " + reas + ".");
}
}
}
The syntax of this code is: /cmd player reason. When I type the reason suppose I typed Spawn killing, it will only type spawn as reason. How to make it correct.
RT Member | Asian City Scripter | CTB Contributor | VKs Contributor | European City Contributor | EAD Scripter

SAzEe21

Hope you have the GetTok And NumTok functions in your script.
If you don't have put these in your script then.
function GetTok(str, sep, pos, len = null)
{
 if (typeof str != "string" || str.len() < 1) return "";
 else if (typeof sep != "string" || sep.len() < 1) return "";
 local tok = split(str, sep);
 if (typeof tok != "array") return str;
 else if (tok.len() == 1 && tok[0] == str) return tok[0];
 if (typeof pos != "integer")
 {
  if (typeof pos == "float" || typeof pos == "string") pos = pos.tointeger();
  else return "";
 }
 if (pos >= tok.len()) return "";
 else if (pos < 0) pos = 0;
 if (len == null)
  len = (tok.len() - pos);
 else if (typeof len != "integer")
 {
  if (typeof len == "float" || typeof len == "string") len = len.tointeger();
  else return "";
 }
 if ((pos+len) > tok.len()) len = (tok.len() - pos);
 else if (len <= 0) return "";
 tok = tok.slice(pos, pos+len);
 if (tok.len() > 1)
 {
  local res = tok[0];
  tok.remove(0);
  foreach (tk in tok) res += sep + tk;
  return res;
 }
 else if (tok.len() == 1) return tok[0];
 else return "";
}

function NumTok(str, sep)
{
 if (typeof str != "string" || str.len() < 1) return 0;
 else if (typeof sep != "string" || sep.len() < 1) return 0;
 local tok = split(str, sep);
 if (typeof tok != "array") return 0;
 else if (tok.len() == 1 && tok[0] == str) return 0;
 else return tok.len();
}

Xmair

Replace
    local reas = GetTok(arguments, " ", 2);
With
    local reas = GetTok(arguments, " ", 2, NumTok(arguments, ""));

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

umar4911

Both didnot work. I have Gettok and Numtok functions
function GetTok(string, separator, n, ...)
{
local m = vargv.len() > 0 ? vargv[0] : n,
tokenized = split(string, separator),
text = "";
if (n > tokenized.len() || n < 1) return null;
for (; n <= m; n++)
{
text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];
}
return text;
}

function NumTok(string, separator)
{
local tokenized = split(string, separator);
return tokenized.len();
}
RT Member | Asian City Scripter | CTB Contributor | VKs Contributor | European City Contributor | EAD Scripter

Xmair

My bad, replace with this:
    local reas = GetTok(arguments, " ", 2, NumTok(arguments, " "));

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

umar4911

RT Member | Asian City Scripter | CTB Contributor | VKs Contributor | European City Contributor | EAD Scripter