# Forum  CS2D Scripts How work the string.sub numbers

# How work the string.sub numbers

4 replies
Goto Page  1  Code:
1
if string.sub(txt, 1,5) == "!ban" then

Can you tell how to use numbers when writing this code?
ohaz
User
Offline There is this awesome thing called google where you can enter "lua string sub" and you will find cool pages like http://lua-users.org/wiki/StringLibraryTutorial that show you how it works.

https://ohaz.engineer - Software Engineering
princeofpersia
User
Offline its wrong > if string.sub(txt, 1,5) == "!ban" then

1 > ! & 2 > b & 3 > a & 4 > n

so its true > if string.sub(txt, 1,4) == "!ban" then

maybe you want use space after !ban . i mean if you use "!ban " insteat "!ban" then > if string.sub(txt, 1,5) == "!ban " then < is true

example :

if string.sub(txt, 1,5) == "!ban " then
pl=(math.floor(string.sub(txt,6,7)))
parse('banip '..pl)
end

now you can use !ban <id> for ban someone
@ princeofpersia:
example :
as far as I know it is true

Code:
1
2
if string.sub(txt, 1, 5)  == "!ban" then
local adam = string.sub(txt, 7, 8)

but if

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if string.sub(txt, 1, 10) == "!ban" then
local adam = string.sub(txt, 12, 13)
return 1
end
elseif string.sub(txt, 1, 2) == "!kick" then
local adam = string.sub(txt, 4, 5)
return 1
elseif string.sub(txt, 1, 7) == "!makect" then
local adam 0 string.sub(txt, 9, 10)
return 1
end

does such excessive and irregular numbers prevent it from functioning?
minos
User
Offline string.sub numbers are only positions of your word for example
this word : slap > we have here 4 letters and with sign ! it's 5
so the strin.sub(1"the first postion in ur cmd","your word letters number")
the commands is !slap
positions are : (1,5) => !(1) , s(2) , l(3) , a(4) , p(5)
then after !slap it will be a space (space's positionis 6) then
id comes after the space and it might be 2 numbers because the maxplayers are 32 then id position is i(7) and d(8)
so the code will be like this
Code:
1
2
3
4
5
6
if string.sub(txt,1,5)=="!slap" then
local kid = tonumber(string.sub(txt,7,8))
parse("slap "..kid)
msg2(id, player(kid,"name").." is slaped")
return 1
end
l  1  ﻿