Function returns 1 argument but not multiple

Anyone able to tell me why this won’t work? when only 1 argument in entered (kick me) it returns it, but when you have multiple, (kick me test) it returns nil. I’m pretty sure the error is in the GetCmd function but cannot work it out.

local function GetCmd(RawInput)
	local Commands = require(script.Server.Commands)
	for Name, Command in pairs(Commands) do
		local CommandName = RawInput:match("^(.+)%s")
		print(CommandName)
		if CommandName == Command.Name then
			local NameLength = string.len(CommandName)
			return Command, RawInput:sub(NameLength + 1)
		else
			return "Invalid Command."
		end
	end
end

local function GetArgs(RawInput)
	print(RawInput)
	local Args = string.split(RawInput, " ")
	for i, v in pairs(Args) do
		if v == nil then
			table.remove(Args, v)
		end
	end
	return Args
end

local ParsedCmd = function(Player, RawInput)
	
	local Command, RawInput = GetCmd(RawInput)
	if Command then
		print(RawInput)
		local Args = GetArgs(RawInput)
		print(Args)
		Command.Execute(Player, Args)
	end
	
end

ClientFunction.OnServerInvoke = ParsedCmd

Can you show me where the error is happening

Can you show the clientside part of your script?

Basically, it uses a local script which communicates to a main module script, which communicates with other module scripts. Local script:


I just ran another test, I think it might be the RawInput:match() line. When I read online, it said it would stop reading the string until it hit whitespace or a ‘space’. Maybe I used this wrong?

take out this line and it’ll work

else
	return "Invalid Command."

You might also want to change the name of one of the RawInput’s, in the ParsedCmd function.

You can also try a pcall to find the error I think

no a pcall just means protected call, meaning it won’t error your code but instead give you a success/error result. It doesn’t find errors for you.

It didn’t work, it printed the command 3 times with. I entered (kick 7h_n yes) as a test and here was the result I was given. Well I didn’t think it would matter since it is using the same variable, just editing the value of it.

well obviously it will print command 3 times, because you have a print statement in that for loop, and there 3 things in the Commands table or there were 3 things before the loop stopped because of return

Should I be using a different method? All I’m trying to do is split up the given text to the first word being the command and the rest being args.

you can split up strings using string.split(string,seperator)

I realized that, but doesn’t that only put it into one table? If I made a variable for the command name, then removed it from the table, the value of the variable would change too right?

I don’t know what you mean

This is how string.split works

local string = "Hello World"

local split_up_string = string.split(string," ") --> returns an array

print(split_up_string[1]) --> prints "Hello"
print(split_up_string[2]) --> prints "World"
1 Like

Well I’d need the first word on its own, then to return the rest of the table without the first word. You’re honestly a life saver thank you for the help.

you can remove anything from the array or use them independently

local prefix = split_up_string[1] -- prefix now equals the first word

table.remove(split_up_string,1) -- remove the first word from the table the rest are arguments

local args = split_up_string

print(prefix) --> prints the first word
print(args) --> prints an array with the rest of the words
2 Likes

It worked, thank you for all the help, I literally spent 5 days trying to figure it out.

1 Like