How to get arguments that have a space?

  1. What do you want to achieve?
    “/setmessage” command that can have multiple args.
  2. What is the issue?
    When I do “/setmessage Test1 Test2 Test3” it only sets the message to “Test1”
  3. What solutions have you tried so far?
    Tried to change i to 15, didn’t work. (for)

Code:

-- [ STARTING VARIABLES ] --

local commands = {}

local prefix = "/" 

local RS = game:GetService("ReplicatedStorage")

local Remote = RS:WaitForChild("Setmessage")

local Remote2 = RS:WaitForChild("SetmessageOFF")

-- [ MAIN CODE ] --

-- Setmessage Command --

commands.setmessage = function(sender, args) -- sender: Object - args : Table
	-- What will happen when you run the command.
	print("Setmessage command ran by:")
	print(sender)
	
	for i, playerName in pairs(args) do
		print(playerName)
	end
	
	local MessageToSet = args[1]
	
	Remote:FireAllClients(MessageToSet)
	
	print("fired event")
 
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message,recipient)
		if player.Name == "airsoft561" then
		message = string.lower(message) -- TP = tp / Tp = tp etc.
		
		local splitString = message:split(" ") -- {":setmessage","message"}
		
		local prefixCmd = splitString[1] -- ":setmessage"
		
		local cmd = prefixCmd:split(prefix) -- {":","setmessage"} -- another table
		
		local cmdName = cmd[2]
		
		
		if commands[cmdName] then
			
			local args = {} -- Other splited, eg: "yeet" if you did: ":setmessage yeet"
			
			for i = 2, #splitString, 1 do
				table.insert(args,splitString[i])
			end
			
			commands[cmdName](player,args) -- Fires function
				
			end
		else
			print("Player not authorized.")
		end
		-- ":setmessage Interviews" ----> {":setmessage","Interviews"} table
	end)
end)

[ANOTHER PROBLEM]
So I realized the chat filter didn’t work even when I didn’t do “/setmessage”!

1 Like

Does it print all arguments correctly? You’re printing each argument but you’re only sending the first one to the clients.

Yes (having them print all lowercase is intentional)
image

The message set is:
image

You need to fire the entire argument table (e.g. with table.concat) and not just the first entry.

You should be able to just replace local MessageToSet = args[1] in commands.setmessage with local MessageToSet = table.concat(args, " ") to get the whole string rather than just the first word. That should work for what you need.

It worked! Thank you! Also, do you know a way I can do it without having all the letters in lower-case? When I type: “TRHC, giving the care you need!” it sets the message to “trhc, giving the care you need!”

You could probably just remove the message = string.lower(message) line and replace local cmdName = cmd[2] with local cmdName = string.lower(cmd[2]) so only the actual command is set to lower case.

Thanks, it worked! We’ll credit you in our credits for helping us.

That’s great! I’m glad it’s working. As well, if you wanted to make the concatenation more efficient, you could use this code. I’m just going to post the whole thing since there are edits in different places.

-- [ STARTING VARIABLES ] --

local commands = {}

local prefix = "/" 

local RS = game:GetService("ReplicatedStorage")

local Remote = RS:WaitForChild("Setmessage")

local Remote2 = RS:WaitForChild("SetmessageOFF")

-- [ MAIN CODE ] --

-- Setmessage Command --

commands.setmessage = function(sender, MessageToSet) -- sender: Object - args : Table
	-- What will happen when you run the command.
	print("Setmessage command ran by:")
	print(sender)
	
	Remote:FireAllClients(MessageToSet)
	
	print("fired event")
 
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message,recipient)
		if player.Name == "airsoft561" then
		
		local splitString = message:split(" ") -- {":setmessage","message"}
		
		local prefixCmd = splitString[1] -- ":setmessage"
		
		local cmd = prefixCmd:split(prefix) -- {":","setmessage"} -- another table
		
		local cmdName = string.lower(cmd[2])
		
		
		if commands[cmdName] then
			
			local MessageToSet = table.concat(args, " ", 2)

			commands[cmdName](player, MessageToSet) -- Fires function
				
			end
		else
			print("Player not authorized.")
		end
		-- ":setmessage Interviews" ----> {":setmessage","Interviews"} table
	end)
end)

This includes the fixed capitalization as well as does the concatenation all in one line rather than making a new table unnecessarily. Also, if you’re planning on using this in-game you may also want to look into text filtering.