Forum

> > CS2D > Scripts > string.sub - check if anything
Forums overviewCS2D overview Scripts overviewLog in to reply

English string.sub - check if anything

2 replies
To the start Previous 1 Next To the start

old string.sub - check if anything

Mami Tomoe
User Off Offline

Quote
Hi everyone I got another problem but I think this one is easier

local reason = string.sub(txt, #p[2]+7, #txt)


This is a line from my ban command (and kick and bc)
This line is also meant to take a reason but if I wont add anything but just say @ban then this will show up:

1
2
3
4
LUA ERROR: sys/lua/sp-rp-script/admin.lua:88: attempt to get length of field '?' (a nil value)
 -> sys/lua/sp-rp-script/admin.lua:88: in function 'ParseAdminCommand'
 -> sys/lua/sp-rp-script/main.lua:221: in function <sys/lua/sp-rp-script/main.lua:210>
 -> in Lua hook 'say', params: 1, '@ban'

So I need to make the command return an error which I did but it doesn't work because of the reason thing.
If you need more parts of the code you can ask, actually I'll just place it all here!

More >

But it would be nice if you tell me how to do it by myself because I think this isn't the last time I'll have this error

old Re: string.sub - check if anything

VADemon
User Off Offline

Quote
The error code says that it didn't find the table entry (p[2] -> (a nil value)) or more precisely #p[2] -> attempt to get length of field (a nil value)

There's another error that goes invisibly: line 3 tonumber(nil) because p[2] equals nil, so your pl variable also may become a nil value.

So you need to check whether p[2] is not nil and only then use it or show an error message.

Here's a one-liner for reason on line 4:
1
local reason = (p[2] ~= nil and string.sub(txt, #p[2]+7, #txt)) or "No Reason Specified"
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview