Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-4

Admin Command Sepration? [closed]

Asked by 10 years ago

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.

0
I have been testing it and for some reason it's removing sentence[3] User#5978 25 — 10y
0
Try in the sentence no.3 add spaces{" "},It may help but i dont know. iLegitus 130 — 10y
0
You must have the problem findw function. Try testing removing and editing and you will eventually fix it a lone. marcoantoniosantos3 200 — 10y
0
You guys are worst helpers ever. It's not findw but good job. findw works perfectlly. User#5978 25 — 10y

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?

2 answers

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Overall style:

  • Tab your code
  • Don't put then on its own line, that doesn't make sense

Your findw function has some issues that I would fix:

  • You shouldn't be defining it inside another function
  • You repeatedly give #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.
  • You can use #msg instead of msg:len(), though this makes little difference

Despite 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.
  • Again, i-1 in table.insert isn't necessary -- it automatically adds to the end
  • Use else 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
Ad
Log in to vote
0
Answered by 10 years ago

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!

0
I fixed it myself, I just called the function out of the brackets. User#5978 25 — 10y