Commands not working

I have a script with commands in server script service:

local commands = {}

local prefix = "/"

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

commands.handto = function(sender,arguments)
	for i, playerName in pairs(arguments) do
		print(playerName)
	end
	
	local customerName = arguments[1]
	
	local inventory = sender:WaitForChild("Backpack")
	local item
	for i, tool in pairs(inventory) do
		if tool.Equipped == true then
			item = tool
		end
	end
	
	if customerName then
		local customer = findPlayer(customerName)
		if customer then
			
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message,recipient)
		local message = string.lower(message)
		local splitString = message:split(" ")
		local slashCommand = splitString[1]
		local command = message:split(prefix)
		local commandName = command[2]
		
		if commands[commandName] then
			local arguments = {}
			for i = 2, #splitString, 1 do
				table.insert(arguments,splitString[i])
			end
			commands[commandName] (player,arguments)
		end
	end)
end)

I am typing,"/handto", but it won’t do anything because it thinks the command isn’t in the list, but it is. How can I fix this?

1 Like

is this a module?
If so do you do a Require?
I ask as it does not return commands

It’s a regular script in server script service. It’s not finished yet because I’m following a tutorial and altering it to what I want the command to do. That’s why it doesn’t do everything you would expect.

Oh wait, I did some more debugging, and I found out that

 message:split(" ")

isn’t splitting the message every time it sees a space. I don’t think it’s detecting that there is a space inside the quote marks. How can I fix this?

Try using

local Split = string.split(message," ")

That didn’t work. It still won’t split it.

Your problem is that you didn’t use a slashCommand. You only cut down “/” from text. So you must edit command to slashCommand:split(prefix)

2 Likes

It works when I put it into studio myself. The only error that was thrown was where you are cycling through the inventory, you need to put

for i, tool in pairs(inventory:GetChildren()) do

Rather than

for i, tool in pairs(inventory) do

Are you getting any other errors when you try it?

1 Like

Thank you! @CharranCZ fixed the main problem, but this fixed a different problem I was going to mention after the main one was fixed. The final known issue is that I think tool.Equipped isn’t looking for if the item is equipped, it is waiting to see if the player equips the item. What property should I use instead?

local Split = string.split(message," ")

and

local Split = message:split(" ")

Are the same thing. Both do the same thing.