Custom Commands experienced an error while loading

I’m following AlvinBlox’s tutorial on making Custom Admin commands. I’m trying to use RegisterProcessCommandsFunction so the commands will delete when executed but for some reason its returning an error.

Here is the code which is in a ModuleScript in the ChatModules Folder:

local commands = {}

local Players = game:GetService("Players")

local prefix = "/"

local function findPlayer(name)

	for i, player in pairs(Players:GetPlayers()) do 
		if string.lower(player.Name) == name then
			return player
		end
	end
	return nil
end

commands.tp = function(speaker, arguments)

	print("Function fired by " ..speaker.Name)

	for i, playerName in pairs(arguments) do
		print(playerName)
	end

	local playerToTeleportName = arguments[1]
	local playerToTeleportToName = arguments[2]

	if playerToTeleportName and playerToTeleportToName then

		local plrToTeleport = findPlayer(playerToTeleportName)
		local plrToTeleportTo = findPlayer(playerToTeleportToName)

		if plrToTeleport and plrToTeleportTo then
			plrToTeleport.Character.HumanoidRootPart.CFrame = plrToTeleportTo.Character.HumanoidRootPart.CFrame
			print("Successfully Moved!")
		end

	end

end

local function OnPlayerAdded(player)

	player.Chatted:Connect(function(message, recipient)
		message = string.lower(message)

		local splitString = message:split(" ")

		local slashCommand = splitString[1]

		local cmd = slashCommand:split(prefix)

		local cmdName = cmd[2]

		if commands[cmdName] then

			local arguments = {}

			for i = 2, #splitString, 1 do
				table.insert(arguments, splitString[i])
			end

			commands[cmdName](player, arguments)

		end
	end)

end

Players.PlayerAdded:Connect(OnPlayerAdded)

local function Run(ChatService)
	ChatService:RegisterProcessCommandsFunction(commands)
end

return Run

I’m getting this error in the output:

Can someone tell me what I’m doing wrong please?

2 Likes

your module’s formatting is wrong,

try doing this:

local function commands.tp(argument whatever)
    code here
end)

It still doesn’t work. :frowning:

Script Analysis now displays these errors:

aight gimme a sec

might not be able to help you rn since
I’m on my phone

Got it. Thanks for trying to help me.

Um, Have you figured it out yet? :frowning:

Are you meant to be returning Run? Instead of Commands.
Because currently it shows you’re returning a function and not a whole module itself.

How would I return the entire module?

Instead of returning a function named Run
you want the table that stores all of its functions.

Example:

commands.set = function()

Local Functions are only accessible through the Module itself unless put into the module exported code.

So like this:

local module = {}
return module 
1 Like

So do I return commands instead?

You do this basically,
Storing commands:

local module = {}

function module.set(arg)
 print(arg)
end -- It'll be exported with any command that is made like this or:

module.set = function(arg)
 print(arg)
end -- module exported commands

return module -- exporting the whole module table.

Local functions that aren’t exported nor accessible unless thru the command itself or stored in the module and is used by the module itself.

1 Like
local commands = {}

local Players = game:GetService("Players")

local prefix = "/"

local function findPlayer(name)

	for i, player in pairs(Players:GetPlayers()) do 
		if string.lower(player.Name) == name then
			return player
		end
	end
	return nil
end

function commands.tp(player, speaker, arguments)
	player.Chatted:Connect(function(message, recipient)
		message = string.lower(message)

		local splitString = message:split(" ")

		local slashCommand = splitString[1]

		local cmd = slashCommand:split(prefix)

		local cmdName = cmd[2]

		if commands[cmdName] then

			local arguments = {}

			for i = 2, #splitString, 1 do
				table.insert(arguments, splitString[i])
			end

			commands[cmdName](player, arguments)

		end
	end)
end

commands.tp = function(speaker, arguments)

	print("Function fired by " ..speaker.Name)

	for i, playerName in pairs(arguments) do
		print(playerName)
	end

	local playerToTeleportName = arguments[1]
	local playerToTeleportToName = arguments[2]

	if playerToTeleportName and playerToTeleportToName then

		local plrToTeleport = findPlayer(playerToTeleportName)
		local plrToTeleportTo = findPlayer(playerToTeleportToName)

		if plrToTeleport and plrToTeleportTo then
			plrToTeleport.Character.HumanoidRootPart.CFrame = plrToTeleportTo.Character.HumanoidRootPart.CFrame
			print("Successfully Moved!")
		end

	end

end


local function Run(ChatService)
	ChatService:RegisterProcessCommandsFunction(commands)
end

return commands

Would this be the correct code?

yep to me seems perfectly great :)!
remember u can use run whenever in the module only or set it to a module command.

1 Like

It still doesn’t work. :frowning:

Its a module script in chat modules btw

whats the error from it?
(character 30)

The Module Script just doesn’t work at all. No errors. Nothing. Its just non-functional.

Bumping this topic because I still really need help on this :frowning:

Bumping this topic again because I still really need help on this… :pensive: