Command module not working correctly

  1. What do you want to achieve? Keep it simple and clear!

I want to the module to know if there is a player arg and what position that would be (“!fly me” would be commandArgs[1] which would be “me”)

  1. What is the issue? Include screenshots / videos if possible!

Whenever I try to execute a command on a specific player, for example: !jail playername, it does not work. However, it works when I use it on myself or on all players at once using key words! (me, all, others.)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have attempted many different things none of which worked. No I have not looked on the dev forum

----- | Services | -----

local Services = _G["Services"]


----- | Variables | -----

local file = _G["File"]
local field = {}; field.__index = field
local temporaryStorage = {}
local Commands = require(script["Command List"])

----- | Methods | -----

function field:SetPrefix(player)
	local Prefix = Services.ReplicatedStorage.FunctionStorage.Prefix:Invoke(player)

	player:SetAttribute(`Prefix`,Prefix)

	return player:GetAttribute(`Prefix`)
end

function field:CheckCommand(command)
	local CommandList = require(script["Command List"])

	if CommandList[command:lower()] or table.find(CommandList,command:lower()) then
		return CommandList[command:lower()]
	end

	for _,MyCommand in pairs(CommandList) do
		if not MyCommand.Alias then 
			if MyCommand.Name:lower() == command:lower() then
				return MyCommand
			end
		else
			if MyCommand.Name:lower() == command:lower() or table.find(MyCommand.Alias,command:lower()) then
				return MyCommand
			end
		end
	end

	return
end

function field:CheckPrefix(player,message:string)
	local prefix = player:GetAttribute(`Prefix`) or field:SetPrefix(player)

	task.wait()

	if string.find(message,prefix) then
		local command = message:gsub(prefix ,"")

		--warn(message:split(" ")[1])

		local Check = field:CheckCommand(command:split(" ")[1])

		task.wait()

		if Check then
			return Check
		else
			return
		end
	end
end

function getPlayerByKeyword(keyword: string, sender: Player)
	local Playerlist = {}

	if keyword:lower():gsub(" ", "") == "me"  and  not table.find(Playerlist, sender) then
		table.insert(Playerlist, sender)
	end

	if keyword:lower():gsub(" ", "") == "all" then
		for _, player in game:GetService("Players"):GetPlayers() do
			if table.find(Playerlist, player) then continue end
			table.insert(Playerlist, player)
		end
	end

	if keyword:lower():gsub(" ", "") == "others" then
		for _, player in game:GetService("Players"):GetPlayers() do
			if table.find(Playerlist, player) or player == sender then continue end
			table.insert(Playerlist, player)
		end
	end

	if keyword:lower():gsub(" ", "") == "me" then
		table.insert(Playerlist, sender)
	end

	if keyword:lower():gsub(" ", "") ~= "me" and keyword:lower():gsub(" ", "") ~= "all" and keyword:lower():gsub(" ", "") ~= "others" then
		for i, player in pairs(Services.Players:GetPlayers()) do
			if string.lower(player.Name) == keyword or string.lower(player.Name):find(keyword) then
				table.insert(Playerlist, player)
			end
		end
	end

	return Playerlist
end

function field:Chatted(sender, message)

	local GetRank = Services[`RankService`]:GetRank(sender)

	repeat task.wait() until GetRank ~= nil

	if sender:GetAttribute(`Rank`) == `None` or GetRank == `None` then  return end

	local MyCommand = field:CheckPrefix(sender,message)

	repeat task.wait() until MyCommand ~= nil


	if MyCommand == nil then return end

	if not table.find(MyCommand.Permissions,sender:GetAttribute(`Rank`)) then Services.ReplicatedStorage.EventStorage:FindFirstChild(`Notify`):FireClient(sender,{Title = `Notice`,Description = `Your rank is not high enough to use the "{MyCommand.Name}" command!`}) return end

	local args = message:lower():split(" ")
	local command = args[1]
	local commandArgs = {}

	if #args > 1 then
		for i = 2, #args do
			local arg = args[i]
			local splitIndex = arg:find(" ") 


			if splitIndex then
				local argName = arg:sub(1, splitIndex - 1)
				local argValue = arg:sub(splitIndex + 1)
				commandArgs[argName] = argValue
			else
				table.insert(commandArgs, arg)
			end
		end
	end

	if table.find(commandArgs,`me`) then
		local players = getPlayerByKeyword(`me`,sender)

		for _,player in pairs(players) do
			local succ,err = pcall(function()
				if MyCommand.Run then
					commandArgs[1] = player
					MyCommand.Run(sender,commandArgs)()
				end    
			end)  
		end
	elseif table.find(commandArgs,`all`) then
		local players = getPlayerByKeyword(`all`,sender)

		for _,player in pairs(players) do
			local succ,err = pcall(function()
				if MyCommand.Run then
					commandArgs[1] = player
					MyCommand.Run(player,commandArgs)()
				end    
			end)    
		end
	elseif table.find(commandArgs,`others`) then
		local players = getPlayerByKeyword(`others`,sender)

		for _,player in pairs(players) do
			local succ,err = pcall(function()
				if MyCommand.Run then
					commandArgs[1] = player
					MyCommand.Run(player,commandArgs)()
				end    
			end)

			if not succ then
				warn(err)
			end
		end
	else
		local succ,err = pcall(function()
			if MyCommand.Run then
				MyCommand.Run(sender,commandArgs)()
			end 
		end)
	end		  
end


----- | Event Listeners | -----

for _,player in pairs(Services.Players:GetPlayers()) do
	player.Chatted:Connect(function(message)
		field:Chatted(player,message)
	end)
end

Services.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		field:Chatted(player,message)
	end)
end)

Services.ReplicatedStorage.EventStorage.ServerCommand.OnServerEvent:Connect(function(player,message)
	field:Chatted(player,message)
end)

return field

Thanks for reading my post.

2 Likes

What is the expected result, and what is the actual result? Rather than just saying “it does not work,” explain how it is not working.

You should only provide the script snippet that pertains to the question, not the entire script, as I don’t think many people want to read through the whole thing. Also, I recommend providing one of your command scripts, as it helps us understand the structure of your project.

1 Like

The expected result is the module identifying if a command needs the player argument and finding it but it only works if you use the key words (me,others,all) therefor you can not use the command on one player.

1 Like

Right, but what makes you say that it does not work? Does it throw an error, say that a value is nil, etc?

2 Likes

If the command doesnt require a player it will still replace the command args w the sender.

1 Like

Command script:

["kick"] = {
		["Name"] = "kick",
		["Description"] = "Kicks a player from the server",
		["Format"] = "!kick <player> <reason>",
		["Alias"] = {},
		["Permissions"] = {"Owner","Head Admin","Admin","Mod"},
		["Run"] = function(sender, commandArgs)
			warn(commandArgs)

			local player = commandArgs[1] or sender
			local reason = table.concat(commandArgs," ",2) or "You were kicked from the game."
			
			player:Kick(reason)
		end,
	},

The issue could be in line 94 where you check whether the player name matches the “keyword” string.

if string.lower(player.Name) == keyword or string.lower(player.Name):find(keyword) then

Here the player’s name is set to lower characters but not and the “keyword” itself.
Which means if the player has any capital letters in his username, even typed correctly, won’ work!

The fix is very simple. The only thing is to convert keyword string to lower characters.

if string.lower(player.Name) == string.lower(keyword) or string.lower(player.Name):find(string.lower(keyword)) then

This way even if the player’s name is typed with any wrong upper or lower letter, won’t make any issues.

Here is the whole corrected script:

----- | Services | -----

local Services = _G["Services"]


----- | Variables | -----

local file = _G["File"]
local field = {}; field.__index = field
local temporaryStorage = {}
local Commands = require(script["Command List"])

----- | Methods | -----

function field:SetPrefix(player)
	local Prefix = Services.ReplicatedStorage.FunctionStorage.Prefix:Invoke(player)

	player:SetAttribute(`Prefix`,Prefix)

	return player:GetAttribute(`Prefix`)
end

function field:CheckCommand(command)
	local CommandList = require(script["Command List"])

	if CommandList[command:lower()] or table.find(CommandList,command:lower()) then
		return CommandList[command:lower()]
	end

	for _,MyCommand in pairs(CommandList) do
		if not MyCommand.Alias then 
			if MyCommand.Name:lower() == command:lower() then
				return MyCommand
			end
		else
			if MyCommand.Name:lower() == command:lower() or table.find(MyCommand.Alias,command:lower()) then
				return MyCommand
			end
		end
	end

	return
end

function field:CheckPrefix(player,message:string)
	local prefix = player:GetAttribute(`Prefix`) or field:SetPrefix(player)

	task.wait()

	if string.find(message,prefix) then
		local command = message:gsub(prefix ,"")

		--warn(message:split(" ")[1])

		local Check = field:CheckCommand(command:split(" ")[1])

		task.wait()

		if Check then
			return Check
		else
			return
		end
	end
end

function getPlayerByKeyword(keyword: string, sender: Player)
	local Playerlist = {}

	if keyword:lower():gsub(" ", "") == "me"  and  not table.find(Playerlist, sender) then
		table.insert(Playerlist, sender)
	end

	if keyword:lower():gsub(" ", "") == "all" then
		for _, player in game:GetService("Players"):GetPlayers() do
			if table.find(Playerlist, player) then continue end
			table.insert(Playerlist, player)
		end
	end

	if keyword:lower():gsub(" ", "") == "others" then
		for _, player in game:GetService("Players"):GetPlayers() do
			if table.find(Playerlist, player) or player == sender then continue end
			table.insert(Playerlist, player)
		end
	end

	if keyword:lower():gsub(" ", "") == "me" then
		table.insert(Playerlist, sender)
	end

	if keyword:lower():gsub(" ", "") ~= "me" and keyword:lower():gsub(" ", "") ~= "all" and keyword:lower():gsub(" ", "") ~= "others" then
		for i, player in pairs(Services.Players:GetPlayers()) do
			if string.lower(player.Name) == string.lower(keyword) or string.lower(player.Name):find(string.lower(keyword)) then -- converting keyword to lower characters
				table.insert(Playerlist, player)
			end
		end
	end

	return Playerlist
end

function field:Chatted(sender, message)

	local GetRank = Services[`RankService`]:GetRank(sender)

	repeat task.wait() until GetRank ~= nil

	if sender:GetAttribute(`Rank`) == `None` or GetRank == `None` then  return end

	local MyCommand = field:CheckPrefix(sender,message)

	repeat task.wait() until MyCommand ~= nil


	if MyCommand == nil then return end

	if not table.find(MyCommand.Permissions,sender:GetAttribute(`Rank`)) then Services.ReplicatedStorage.EventStorage:FindFirstChild(`Notify`):FireClient(sender,{Title = `Notice`,Description = `Your rank is not high enough to use the "{MyCommand.Name}" command!`}) return end

	local args = message:lower():split(" ")
	local command = args[1]
	local commandArgs = {}

	if #args > 1 then
		for i = 2, #args do
			local arg = args[i]
			local splitIndex = arg:find(" ") 


			if splitIndex then
				local argName = arg:sub(1, splitIndex - 1)
				local argValue = arg:sub(splitIndex + 1)
				commandArgs[argName] = argValue
			else
				table.insert(commandArgs, arg)
			end
		end
	end

	if table.find(commandArgs,`me`) then
		local players = getPlayerByKeyword(`me`,sender)

		for _,player in pairs(players) do
			local succ,err = pcall(function()
				if MyCommand.Run then
					commandArgs[1] = player
					MyCommand.Run(sender,commandArgs)()
				end    
			end)  
		end
	elseif table.find(commandArgs,`all`) then
		local players = getPlayerByKeyword(`all`,sender)

		for _,player in pairs(players) do
			local succ,err = pcall(function()
				if MyCommand.Run then
					commandArgs[1] = player
					MyCommand.Run(player,commandArgs)()
				end    
			end)    
		end
	elseif table.find(commandArgs,`others`) then
		local players = getPlayerByKeyword(`others`,sender)

		for _,player in pairs(players) do
			local succ,err = pcall(function()
				if MyCommand.Run then
					commandArgs[1] = player
					MyCommand.Run(player,commandArgs)()
				end    
			end)

			if not succ then
				warn(err)
			end
		end
	else
		local succ,err = pcall(function()
			if MyCommand.Run then
				MyCommand.Run(sender,commandArgs)()
			end 
		end)
	end		  
end


----- | Event Listeners | -----

for _,player in pairs(Services.Players:GetPlayers()) do
	player.Chatted:Connect(function(message)
		field:Chatted(player,message)
	end)
end

Services.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		field:Chatted(player,message)
	end)
end)

Services.ReplicatedStorage.EventStorage.ServerCommand.OnServerEvent:Connect(function(player,message)
	field:Chatted(player,message)
end)

return field

I hope I was useful! If it didn’t work, I have a mistake or something else, fell free to reply! :slight_smile:

1 Like

This still doesnt work

image (Fake verified badge)

The other prams work!

Weird.

Can you try printing the keyword and see what is gives?
Also it would be good if you also print the players in the game from the module Services.

Can you give the output of this script:

----- | Services | -----

local Services = _G["Services"]


----- | Variables | -----

local file = _G["File"]
local field = {}; field.__index = field
local temporaryStorage = {}
local Commands = require(script["Command List"])

----- | Methods | -----

function field:SetPrefix(player)
	local Prefix = Services.ReplicatedStorage.FunctionStorage.Prefix:Invoke(player)

	player:SetAttribute(`Prefix`,Prefix)

	return player:GetAttribute(`Prefix`)
end

function field:CheckCommand(command)
	local CommandList = require(script["Command List"])

	if CommandList[command:lower()] or table.find(CommandList,command:lower()) then
		return CommandList[command:lower()]
	end

	for _,MyCommand in pairs(CommandList) do
		if not MyCommand.Alias then 
			if MyCommand.Name:lower() == command:lower() then
				return MyCommand
			end
		else
			if MyCommand.Name:lower() == command:lower() or table.find(MyCommand.Alias,command:lower()) then
				return MyCommand
			end
		end
	end

	return
end

function field:CheckPrefix(player,message:string)
	local prefix = player:GetAttribute(`Prefix`) or field:SetPrefix(player)

	task.wait()

	if string.find(message,prefix) then
		local command = message:gsub(prefix ,"")

		--warn(message:split(" ")[1])

		local Check = field:CheckCommand(command:split(" ")[1])

		task.wait()

		if Check then
			return Check
		else
			return
		end
	end
end

function getPlayerByKeyword(keyword: string, sender: Player)
	local Playerlist = {}
	
	print(keyword) -- printing

	if keyword:lower():gsub(" ", "") == "me"  and  not table.find(Playerlist, sender) then
		table.insert(Playerlist, sender)
	end

	if keyword:lower():gsub(" ", "") == "all" then
		for _, player in game:GetService("Players"):GetPlayers() do
			if table.find(Playerlist, player) then continue end
			table.insert(Playerlist, player)
		end
	end

	if keyword:lower():gsub(" ", "") == "others" then
		for _, player in game:GetService("Players"):GetPlayers() do
			if table.find(Playerlist, player) or player == sender then continue end
			table.insert(Playerlist, player)
		end
	end

	if keyword:lower():gsub(" ", "") == "me" then
		table.insert(Playerlist, sender)
	end

	if keyword:lower():gsub(" ", "") ~= "me" and keyword:lower():gsub(" ", "") ~= "all" and keyword:lower():gsub(" ", "") ~= "others" then
		print(Services.Players:GetPlayers()) -- printing
		for i, player in pairs(Services.Players:GetPlayers()) do
			if string.lower(player.Name) == string.lower(keyword) or string.lower(player.Name):find(string.lower(keyword)) then -- converting keyword to lower characters
				table.insert(Playerlist, player)
			end
		end
	end

	return Playerlist
end

function field:Chatted(sender, message)

	local GetRank = Services[`RankService`]:GetRank(sender)

	repeat task.wait() until GetRank ~= nil

	if sender:GetAttribute(`Rank`) == `None` or GetRank == `None` then  return end

	local MyCommand = field:CheckPrefix(sender,message)

	repeat task.wait() until MyCommand ~= nil


	if MyCommand == nil then return end

	if not table.find(MyCommand.Permissions,sender:GetAttribute(`Rank`)) then Services.ReplicatedStorage.EventStorage:FindFirstChild(`Notify`):FireClient(sender,{Title = `Notice`,Description = `Your rank is not high enough to use the "{MyCommand.Name}" command!`}) return end

	local args = message:lower():split(" ")
	local command = args[1]
	local commandArgs = {}

	if #args > 1 then
		for i = 2, #args do
			local arg = args[i]
			local splitIndex = arg:find(" ") 


			if splitIndex then
				local argName = arg:sub(1, splitIndex - 1)
				local argValue = arg:sub(splitIndex + 1)
				commandArgs[argName] = argValue
			else
				table.insert(commandArgs, arg)
			end
		end
	end

	if table.find(commandArgs,`me`) then
		local players = getPlayerByKeyword(`me`,sender)

		for _,player in pairs(players) do
			local succ,err = pcall(function()
				if MyCommand.Run then
					commandArgs[1] = player
					MyCommand.Run(sender,commandArgs)()
				end    
			end)  
		end
	elseif table.find(commandArgs,`all`) then
		local players = getPlayerByKeyword(`all`,sender)

		for _,player in pairs(players) do
			local succ,err = pcall(function()
				if MyCommand.Run then
					commandArgs[1] = player
					MyCommand.Run(player,commandArgs)()
				end    
			end)    
		end
	elseif table.find(commandArgs,`others`) then
		local players = getPlayerByKeyword(`others`,sender)

		for _,player in pairs(players) do
			local succ,err = pcall(function()
				if MyCommand.Run then
					commandArgs[1] = player
					MyCommand.Run(player,commandArgs)()
				end    
			end)

			if not succ then
				warn(err)
			end
		end
	else
		local succ,err = pcall(function()
			if MyCommand.Run then
				MyCommand.Run(sender,commandArgs)()
			end 
		end)
	end		  
end


----- | Event Listeners | -----

for _,player in pairs(Services.Players:GetPlayers()) do
	player.Chatted:Connect(function(message)
		field:Chatted(player,message)
	end)
end

Services.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		field:Chatted(player,message)
	end)
end)

Services.ReplicatedStorage.EventStorage.ServerCommand.OnServerEvent:Connect(function(player,message)
	field:Chatted(player,message)
end)

return field

I cannot test in studio because I get an error, more info on that here.

EDIT: You can find and use the main module here.

(Links removed)

@EthicalEye have you managed to find out why this is happening?

Edit: that did not print anything at all.

Rescripted the system and also got help from DonKingFrog

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