Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Admin Command Sepration?

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.

4 comments

0

I have been testing it and for some reason it's removing sentence[3]

0

Try in the sentence no.3 add spaces{" "},It may help but i dont know.

0

You must have the problem findw function. Try testing removing and editing and you will eventually fix it a lone.

0

You guys are worst helpers ever. It's not findw but good job. findw works perfectlly.

2 answers

✓ Accepted answer

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

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!

1 comment

0

I fixed it myself, I just called the function out of the brackets.

This discussion is preserved as it was. New answers, comments, and votes are closed.