Arguments and splitting

I’m attempting to make a command that’s specifically designated to the whitelisted personnel, but it looks like it doesn’t operate and I didn’t retrieve a clear instruction on how to function is as intended. About this command, when I state something as “/role doq yes”, it’ll appear in the billboard’s textlabel as “yes” and in the leaderstats as “yes”, but it doesn’t operate at all. What’s the concurrent issue? Here’s the code I have currently written:

local cmdAccess = {"Doqee"}
game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
	msg = string.lower(msg)
		for _, administrator in ipairs(cmdAccess) do
			if plr.Name == administrator then
				if msg:sub(1, 6) == "/role " then
					local aim = game.Players:FindFirstChild(msg:sub(7))
                    if aim then
						local text = msg(8)
                        aim.Character.Head.UserTitle.Role = text
                        aim.leaderstats.Role.Value = text
					end
				end
			end
		end
	end)
end)

I don’t completely understand arguments and such, as this is my beginning experience on them, but I want to expand my knowledge for them - so if possible, please elaborate on it.

You can use string:split for cases like this.

Here’s an example:

local Players = game:GetService('Players')
local Prefix = '/'

local Access = {123, 1234, 1235} -- userids, since they have the ability to change their names.

local function HasPermission(UserId)
	for _,v in ipairs(Access) do
		if UserId == v then
			return true
		end
		return false
	end	
end

Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		local Args = Message:split(' ')
		local Arg1, Arg2 = Args[1], Args[2]
		
		if Arg1:lower() == Prefix .. 'role' and Arg2 and Arg2:find('%w') then -- check if the desired role name contains a alphanumeric character (0-9, a-z) 
			if HasPermission(Player.UserId) then
				YourTextHere.Text = Arg2
			end	
			 -- arg1: /role
			-- arg2: second argument (after /role)
		end
	end)
end)

I’ve had my own troubles with commands and such, and they can be really annoying. But, I would recommend making a system of getting arguments automatically.

local commands = {
    hello = function() end
}
local function getCommand(message) --get command
    for name, command in pairs(commands) do
        local nameLength = string.len(name) --get the length of the name
        
        if message:sub(0, nameLength + 1) == name then --If the first part of the message equals the command's name, then that's it
            return command, message:sub(nameLength + 1) --return the command and the message without the name
        end
    end
end

local function getArguments(message)
    local args = {}
    local arg = 0
    
    for text in message:gmatch("%S+") do --this will give ALL the arguments after each space
        arg = arg + 1
        
        args[arg] = text
    end
end

local function playerChatted(player, message)
    local isCommand = message:sub(0, 2) == "/" --if the first character is slash then it might be a command
    
    message = message:sub(2) --take off the slash
    
    if isCommand then
        local command, message = getCommand(message)
        
        if command then
            local arguments = getArguments(message)
            
            command(arguments) --fire the command
        end
    end
end

Wow! I’ve never noticed there was a split function! This is a much simpler way than mine. :open_mouth:

Your method using gmatch is actually better. split doesn’t seem to accept patterns so it will add an empty string in cases where there is more than one space between words.