Can not convert string

Started by Cool, Jul 21, 2016, 12:57 PM

Previous topic - Next topic

Cool

function GiveSpawnwep(player)
{
 if (SpawnwepPlayer[ player.ID ] != null)
 {
  local i = 1, khe = SpawnwepPlayer[ player.ID ], Nweps = NumTok( khe," ");
  while ( i < (Nweps.tointeger()+1))
  {
   local ID = GetTok( khe," ", i);
   player.SetWeapon( ID.tointeger(), 99999 ); i++;
  }
  ClientMessage("-> [#83bab3][SPAWNWEP] Weapons setting in spawn, Completed successfully.",player,255,0,102);
 }
}
error line    player.SetWeapon( ID.tointeger(), 99999 ); i++;

vito

do print(typeof(ID)) before this line

KAKAN

such a script, never saw that lol. Use a foreach loop
Before using this script, try what is recommended by Vito
foreach( shit in SpawnwepPlayer[ player.ID ] ) player.SetWeapon( shit.tointeger(), 9999 );
oh no

Cool

@KAKAN i am confused in one talk thats works on vps with no error and not work in pc why :P

KAKAN

Quote from: Hercules on Jul 21, 2016, 01:39 PM@KAKAN i am confused in one talk thats works on vps with no error and not work in pc why :P
just do this: print( typeof ID ); in your while loop and tell me the result
And also, add this: print( ID )
oh no

DizzasTeR

This is not Lua, this is squirrel and squirrel's .tointeger() doesn't work if the characters inside the string are not only numbers. To simplify:
local myString = "Something";
local numString = "123";

print( numString.tointeger() ); -> Prints: 123 Type: Integer
print( myString.tointeger() ); -> Prints: Error: cannot convert string

KAKAN

Quote from: Doom_Kill3R on Jul 21, 2016, 01:47 PMThis is not Lua, this is squirrel and squirrel's .tointeger() doesn't work if the characters inside the string are not only numbers.
He claims that it works on the VPS and not on his PC, so he must have altered something on his PC. That's why we're poking him to print typeof ID so we can see what's there inside the string.
oh no

DizzasTeR

You can try running the code I gave on both Windows and Linux and you will get the same results. His problem is
local ID = GetTok( khe," ", i);
player.SetWeapon( ID.tointeger(), 99999 );

He is storing spawnweps in a string which is concatenated by space, so lets say I have picked Stubby and a Colt, but for Stubby I didn't use ID, I used Name instead so the SpawnwepPlayer for me will be:
SpawnwepPlayer[ myID ] = "Stubby 17"; // 17 is colt

Now when the loop runs, which is simply splitting that string with " " and then taking it as integer the same case will occur which I posted above:
// i = 1
local ID = GetTok( khe," ", i); // returns: "Stubby"
player.SetWeapon( ID.tointeger(), 99999 ); // Error, doing tointeger on "Stubby" which is not a number
i++;

:edit: The solution is simple, you check if ID IsNum or not and if it is a number then use the same code, if its NOT a number then use GetWeaponFromName, and then set that weapon for player and it will work