Making a command system

Does anyone know how to make a command in this format?
/add person money 100

i need to know basically how to make this detect when a word is over.
local arguement = string.sub(msg, 6,?)

This is what i have right now. Sorry if this is very messy.

local admins = {
	1416574751
}

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		
		if table.find(admins,player.UserId) then
			if string.sub(msg, 1, 5) == "/add " then
				local arguement = string.sub(msg, 6)
				local stat = string.sub(msg,string.len(arguement)+2)
				local amount = string.sub(msg,string.len(stat)+2)

				if string.len(arguement)>=3 then
					for i,v in pairs(game.Players:GetChildren()) do
						if (string.match(string.lower(v.Name),string.lower(arguement)) or string.match(string.lower(v.DisplayName),string.lower(arguement))) and stat ~= nil and amount ~= nil then
							v.leaderstats[stat].Value = amount
							local clone = game.ReplicatedStorage.AdminNotification:Clone()
							clone.Parent = player.PlayerGui.Ui
							clone.TextLabel.Text = "set "..v.Name.."'s "..stat.." to "..amount
							clone.TextLabel.TextColor3 = Color3.fromRGB(255,255,255)
						elseif stat == nil then
							local clone = game.ReplicatedStorage.AdminNotification:Clone()
							clone.Parent = player.PlayerGui.Ui
							clone.TextLabel.Text = "please provide a message like '/stat name money 100'"
							clone.TextLabel.TextColor3 = Color3.fromRGB(255,0,0)
						elseif amount == nil then
							local clone = game.ReplicatedStorage.AdminNotification:Clone()
							clone.Parent = player.PlayerGui.Ui
							clone.TextLabel.Text = "please provide a message like '/stat name money 100'"
							clone.TextLabel.TextColor3 = Color3.fromRGB(255,0,0)
						else
							local clone = game.ReplicatedStorage.AdminNotification:Clone()
							clone.Parent = player.PlayerGui.Ui
							clone.TextLabel.Text = "there is no player under the name of '"..arguement.."'"
							clone.TextLabel.TextColor3 = Color3.fromRGB(255,0,0)
						end
					end
				else
					local clone = game.ReplicatedStorage.AdminNotification:Clone()
					clone.Parent = player.PlayerGui.Ui
					clone.TextLabel.Text = "please provide a more specific name"
					clone.TextLabel.TextColor3 = Color3.fromRGB(255,0,0)
				end

			end
		end 
		
	end)
end)
2 Likes

What you’re looking for here is string.split().

string.split() splits a string into parts using a supplied separator as its argument and returns an array of separate pieces the string was split into. The separator here being an empty space " " due to it’s consistent pattern across chat messages and commands.

The code below should give you a brief idea how to go about implementing it.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		local split = msg:split(" ") -- split the message into an array containing each separate word
		local command, target, stat, amount = split[1]:lower(), split[2], split[3], split[4]
		
		if command == "/add" then
			if target and stat and amount then
				-- Execute certain code
			end
		elseif command == "/remove" then
			-- Command-specific checks and executution
		end
	end)
end)
2 Likes