Admin Script not working

Alright, so my admin commands script is kinda not working, here is the script.

Module Script:

local Commands = {}

Commands.tp = function()
	print('This is a success of the teleport command.')
end

return Commands

ServerScriptService script:

local prefix = '?'
local commands = require(script.Commands)

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		local splitString = message:split(' ') -- Splits the message as obvious
		local fCommand = splitString[1]:split(prefix) -- Fired Command variable
		
		if commands[fCommand] then
			commands[fCommand[2]]()
		end
	end)
end)

The module script is a child of the ServerScriptService script.
If possible please explain on what I did wrong.
The script name of the ServerScriptService script is Admin Commands

Could you explain what not working means in your case? Are there any errors? Does the code not respond? Could you also debug with print?

The code doesn’t respond with the print function in the Module Script.

Nope, the code still doesn’t give a response nor a error message.

Okay then use print inside the chatted event to see where the code stops. Probably the if statement is not working.

No, I added this:

local commands = require(script.Commands)
local prefix = '?'

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		local splitString = message:split(' ') -- Splits the message as obvious
		local fCommand = splitString[1]:split(prefix) -- Fired Command variable
		print('Chatted!')
		if commands[fCommand] then
			commands[fCommand[2]]()
		end
	end)
end)

And it printed ‘Chatted!’ just as the print says.

Try this?

local prefix = '?'
local commands = require(script.Commands)

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if message:match("^%"..prefix) then
            local args = message:split(" ")
            local command = args[1]:gsub(prefix, ""):lower()
            table.remove(args, 1)
            if commands[command] then
                -- Clean Args
                for i, v in pairs(args) do
                    if i > 10 then args[i] = nil end
                    if tonumber(v) then args[i] = tonumber(v) end
                end
                -- Run Command
                commands[command](args)
            end
        end
	end)
end)
1 Like

Ah, thank you very much this worked! Now just 1 thing, do I need to re-write that everytime I add a command or is that synced automatically?

It is not hard-coded, if that’s what you mean.

1 Like

I know this is solved but here’s what you did wrong.

fCommand is the response of the split function which is always a table

Now i tested in studio and fCommand is

{
[1] = "",
[2] = "tp"
}

So you’re indexing commands[{[1] = "", [2] = "tp"}] which does not exist
(table keys do exist.)

Thank you very much, for the post!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.