Chat Command Issue

  1. What do you want to achieve? I want to create a command system in which when you write !warn [playername] [reason] it’ll send that to Discord and you’ll be able to access it from there.

  2. What is the issue? I already have the Roblox-to-Discord working, just wondering how I would display the reason, when a admin sends the reason, it’ll be more than 1 word, so I’m trying to see how to add those words without needing the call them 1 by 1. Anyways, here’s my script.

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(message)
		if string.find(message, "!warn") then
			local command = {}
			for word in string.gmatch(message, "%S+") do
				table.insert(command, word)
			end

			local player = command[2]
			local reason = command[3] -- What I want to change; not sure what do change it to though.
			local Webhook = "not shown for obvious reasons :)"
			local HttpService = game:GetService("HttpService")
			
			local discordMessage = {
				['embeds'] = {{
					['title'] = Player.Name.." warned "..player,
					['description'] = reason,
					['color'] = 16744576
				}}
			}
			
			local finaldiscordMessage = HttpService:JSONEncode(discordMessage)
			HttpService:PostAsync(Webhook, finaldiscordMessage)
		end
	end)
end)

If anyone knows how to achieve what I’m trying to do please let me know.

There are two ways that can I do this off the top of my head.

The first is by using table.concat, which is probably the easier way to do it.

local reason = table.concat(command, " ", 3, #command)
print(reason)

The other way is by doing it manually if table.concat isn’t working for whatever reason.

local reason = ""
for index, word in ipairs(command) do
    -- // If not the first two words, add the word to the reason
    if (index ~= 1 and index ~= 2) then
        reason = reason .. word
        -- // If not the last word, add a space
        if (index ~= #command) then
            reason = reason .. " "
        end
    end
end

print(reason)
1 Like