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
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?
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
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?
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"
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