Help making custom commands

Hello!

I’ve been struggling creating a script the past few days and I have finally admitted defeat. I am not a scripter at all but I am looking to improve.

I am trying to make a script that allows you to run a command (!iso [USERNAME] [ REASON]) which would change a players team and respawn them so they spawn in their new team spawn. Then I want this to mute the player and send a webhook message to a server that looks a bit like this example I made using DiscoHook.

image

Now, I would like to clarify I am not asking somebody to create whole scripts for me; I want to improve my own scripting skills for the future so I would prefer you explain the components I would need to use and what they do in simple terms (I struggle to understand the Roblox articles on these)

Thanks!!

1 Like

it would help to show us what you did so far

Okay so, first of all you need a command handler, I’ll make a simple one for you but this is definitely not the most effective one you can do

function sendWebhook(url, content)
	print(content)

    -- please keep in mind you have to do this part yourself, there are websites you can use which help
end

local url = "" -- webhook url
local Prefix = "!" -- any prefix you want

local Admins = {"Player1", "Player2", "Player3"} -- player name of admin table

local Players = game:GetService('Players')

for i,v in pairs(Players:GetPlayers()) do
	if table.find(Admins, v.Name) then -- check if player is in the admin table
		v.Chatted:Connect(function(msg) -- chatted function
			local checkCommand = string.match(msg, "^"..Prefix) -- check for prefix
			if checkCommand then
				msg = string.gsub(msg, checkCommand, "", 1) -- change it to just command
				
				local arguments = {}

				for argument in string.gmatch(msg, "[^%s]+") do -- add to arguments if there is a space (referenced from another devforum wiki)
					table.insert(arguments, argument)
				end
				
				
				local command = arguments[1]
				
				if command == "iso" then -- check for command
					local isolatedBy = v.Name -- admin name
					local isolatedStudent = arguments[2] -- isolated person name
					local isolatedReason = arguments[3] -- isolated reason text
					
					sendWebhook(url, "Isolated Student: "..isolatedStudent.." Isolated By: "..isolatedBy.." Reason: "..isolatedReason) -- what you would do (not exact just reference)
				end
			else
				-- do nothing
			end
		end)
	end
end

To make it easier for you on the string manipulation area

"^"..Prefix simply checks for the prefix at the start of the message which is why there is ^ (it is the head of the string)

"[^%s]+" (the %s simply means a space, and the [] is used for sets, which means multiple checks, so a set of the head of the string,(so a text) then the space and when set is closed, the + which checks if one or more of the character class appears, since it’s inside a set, it will apply to both, the head of the string and the space, without it it’ll just be the space.

By adding these into an argument table, we can easily check for the commands, and be more flexible with the command handler.

Oh and for the table checking, I simply used table.find, you can use an if check inside a function if you want, but this is just less work so.

I hope you’re able to make what you need, and that this helped. Good luck!

1 Like

Thank you so much, this is amazing. This is easy to read and understand and that means a lot to me. Have a nice evening!! :slight_smile:

1 Like

No problem mate! Glad it helped, have a good day!