Help with making my chat command work

Hi, Im making a custom admin commands and I have a custom command that will have a message pop up on everybody’s screens containing the text the player said in chat here is my code:

commands.message = function(sender, arguments)
	print("Message Command Fired By "..sender.Name)
	local message = Instance.new("Message")
	message.Text = sender.Name.." Said: ".. -- This is where the message text is
	message.Parent = game.Workspace
	wait(5)
	message.Parent = nil
end

Thanks.

commands.message = function(sender, Message)
	print("Message Command Fired By "..sender.Name)
	local message = Instance.new("Message")
	message.Text = sender.Name.." Said: "..Message
	message.Parent = game.Workspace
	task.wait(6)
	message.Parent = nil
end
1 Like

I get an error saying:

ServerScriptService.CustomAdmin:66: attempt to concatenate string with table
1 Like

What is “Message” represented by when called ?
I assume it is a table, what does it contain?

Try this:

commands.message = function(sender, arguments)
	print("Message Command Fired By "..sender.Name)
	local message = Instance.new("Message")
	message.Text = sender.Name.." Said: "..arguments["Message"]
	message.Parent = game.Workspace
	task.wait(5)
	message.Parent = nil
end

In here I assume you have a “Message” in your table when calling/requiring the module.

1 Like

Here is my entire admin script:

local commands = {}
local prefix = "/"

local admins = {
	"PuffyJasonrocks84";
	"1BATGIRLL"
}

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

local function isAdmin(player)
	for _, v in pairs(admins) do
		if v == player.Name then
			return true
		end
	end
	return false
end



commands.tp = function(sender, arguments)
	
	print("TP Function Fired by"..sender.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 plrToTp = findplayer(playerToTeleportName)
		local plrToTPTo = findplayer(playerToTeleportToName)
		
		if plrToTp and plrToTPTo then
			plrToTp.Character.HumanoidRootPart.CFrame = plrToTPTo.Character.HumanoidRootPart.CFrame
			print("Successfully Moved!")
		end
		
	end	
	
end


commands.message = function(sender, arguments)
	print("Message Command Fired By "..sender.Name)
	local message = Instance.new("Message")
	message.Text = sender.Name.." Said: "..arguments["Message"]
	message.Parent = game.Workspace
	task.wait(5)
	message.Parent = nil
end




commands.speed = function(sender, arguments)
	
	print("Speed Command Fired By"..sender.Name)
	
	local playerToGiveSpeedTo = arguments[1]
	local amountofspeedtogive = arguments[2]
	
	if playerToGiveSpeedTo then
		local plr = findplayer(playerToGiveSpeedTo)
		
		if plr then
			plr.Character.Humanoid.WalkSpeed = tonumber(amountofspeedtogive)
			print(playerToGiveSpeedTo.." was given WalkSpeed "..amountofspeedtogive)
		end
	end
end


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

I see. You could make it so the second index will be the message.


commands.message = function(sender, arguments)
	print("Message Command Fired By "..sender.Name)
	local message = Instance.new("Message")
	message.Text = sender.Name.." Said: "..arguments[2]
	message.Parent = game.Workspace
	task.wait(5)
	message.Parent = nil
end

You might need to make little changes if need

i learned this from the last post

game.Players.PlayerAdded:Connect(function(dude)
	dude.Chatted:Connect(function(String)
		if string.find(String, "/message") then
			local Message = string.split(String, "/message ")

			print(Message[2])
		end
	end)
end)

when you say /message AAAAAAAAAAAAAAA

it will only print AAAAAAAAAAAAAAA

then you can say something like /e /message AAAAAAAAAAAAAAA

1 Like

Yeah I still get the error:

ServerScriptService.CustomAdmin:60: attempt to concatenate string with nil

Thanks

I tried this but it only prints the message in the output instead of a screen message so I added:

local M = Instance.new("Message")
	M.Text = sender.Name.." Said "..Message

and it gave me an error saying:

ServerScriptService.CustomAdmin:62: attempt to index nil with 'Name'

The full code:

game.Players.PlayerAdded:Connect(function(dude, sender, arguments)
	dude.Chatted:Connect(function(String)
		if string.find(String, "/message") then
			local Message = string.split(String, "/message")
			local M = Instance.new("Message")
			M.Text = sender.Name.." Said "..Message
			print(Message[2])
		end
	end)
end)

i mean this

game.Players.PlayerAdded:Connect(function(dude, sender, arguments)
	dude.Chatted:Connect(function(String)
		if string.find(String, "/message") then
			local Message = string.split(String, "/message")
			local M = Instance.new("Message")
			M.Text = dude.Name .." Said ".. Message[2]
			print(dude.Name .." Said: ".. Message[2]) -- for testing
		end
	end)
end)
1 Like

now it does not give any errors but also does not show the message on screen, weird.

you forgot to change the parent

local M = Instance.new("Message")
M.Parent = workspace
1 Like

yeah I just realized this as I posted this comment thx lol

1 Like

Your “arguments” returns a table, therefore you need to get the first item in the Table.

  08:02:26.849   ▼  {
                    [1] = "hello" -- first item in the table
                 }  -  Server - Script:57
  08:02:26.849  Message Command Fired By LemonElk1113179

You can do this by using pointers instead of “Message” which is a string.

message.Text = sender.Name.." Said: ".. arguments[1] -- first pointer

Here, Try this:

commands.message = function(sender, arguments)
	print("Message Command Fired By "..sender.Name)
	local message = Instance.new("Message")
	message.Text = sender.Name.." Said: ".. arguments[1]
	message.Parent = game.Workspace
	task.wait(5)
	message.Parent = nil
end

Edit: Explaination.

1 Like