I am making my own admin command system and at the moment I am having problems with the word seperator.
function admin_command(text) function findw(msg,var) spaces = {} for i = 0, msg:len() do if msg:sub(i,i) == var then table.insert(spaces,#spaces+1,i) end end table.insert(spaces,#spaces+1,msg:len()+1) sentence = {} for i = 1, #spaces do if i > 1 then table.insert(sentence,#sentence+1,msg:sub(spaces[i-1]+1,spaces[i]-1)) end if i == 1 then table.insert(sentence,#sentence+1,msg:sub(0,spaces[i]-1)) end end return sentence end sentence = findw(text," ") groups = {""} for i = 2, #sentence do if string.find(sentence[i],",") ~= nil then names = findw(sentence[i],",") tab = {} for i = 1, #names do table.insert(tab,#tab+1,names[i]) end table.insert(groups,i-1,{tab,"Group"}) elseif string.find(sentence[i],",") == nil then table.insert(groups,i-1,{sentence[i],"Single"}) end end end admin_command(";tp me,chicken gold lol")
Workspace.Script:27: bad argument #1 to 'find' (string expected, got nil) error message
I have no idea what's wrong with it, string.find should work.
Overall style:
then
on its own line, that doesn't make senseYour findw
function has some issues that I would fix:
#tab + 1
to table.insert
despite that being the default value -- this doesn't make it more clear, it makes it look like it's supposed to be important.#msg
instead of msg:len()
, though this makes little differenceDespite that, there's a much shorter, simpler solution to implement findw
:
function findw(text, separator) local words = {} for word in text:gmatch("[^" .. separator .. "]+") do table.insert(words, word) end return words end
Now the other function:
groups
should start as empty not containing an empty string.if string.find(sentence[i], ",") ~= nil
could be written as the much more natural if sentence[i]:find(",")
tab
is the same thing as names
-- just don't make it.i-1
in table.insert
isn't necessary -- it automatically adds to the endelse
not elseif
-- there's absolutely no reason to explicitly negate yourself. Ending with elseif
not else
implies that sometimes none of the things will happen.admin_command
doesn't do anything or return anything. It should probably return groups
at the end.I would also suggest dropping the whole "Group" and "Single" business. findw
will just return a list of element, so if for some reason the behavior is different, you could just check the length of the array.
But most things (e.g., hurt, teleport, etc) would just be mapped over all of the elements of the list, so it makes more sense to not have to write two cases later on.
function admin_command(text) sentence = findw(text," ") groups = {} for i = 2, #sentence do table.insert(groups, findw(sentence[i], ",")) end return groups end
The problem is line 2: It is not returning anything, not a Boolean, Table, String, or Number value! This is a simple fix, let me explain;
Let's use the return keyword/method. Why? Because as your script is now with that function, IT IS NOT returning anything. By using the return
keyword, we can return spaces
when the function is being executed, also, you forgot an end
in your function;
function findw(msg,var) --Your function spaces = {} --This is the table you are currently using for i = 0, msg:len() do --Get's the length of the string size, then loops through it if msg:sub(i,i) == var then --Will check if there is a match in the string table.insert(spaces,#spaces+1,i) --Will insert these into the table 'spaces' end --Ends the code block for the 'if' statement end --Ends the code block for the 'for' loop return spaces --This will return the table 'spaces' end --This ends the code block for the function
Hope this helped!
Locked by TheeDeathCaster, Redbullusa, and M39a9am3R
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?