How would I get it to print the message/command?s

Hey I am currently making an admin logging system yet it isn’t going as planned.

I am trying to print out the message if the message contains a sub-part of the command.
Ex: “/h”, “/kick”, etc.

The commands do not contain the slash (“/”) as it is given the slash in the admin script so it is a plain letter(s) or word.

It doesn’t give an error yet doesn’t print anything.

Here is my script:

local rs = game:GetService("ReplicatedStorage")
--local event = rs.Events.chatEvent

local commands = script.Parent.AdminSystem.Commands:GetChildren() --Gets the folder in which the commands are placed.


game:GetService("Players").PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == commands then
			print(msg)
		end
	end)
end)

Help would really be appreciated it.

1 Like

Your problem is pretty straightforward: Obviously, the msg is not equal to a table of all the commands there are.

What you want to do is check if msg is IN commands, not if msg IS commands (difference between “I am a player of roblox” and “I am roblox” lol)

To check if it is in the table, just do table.find(commands, msg) (It should return a truthy value if it is in there and a falsey value if it isn’t, im too lazy to check what the exact values are but you can just do “if table.find(commands, msg) then stuff”)

Are you sure the commands variable is a table that has a string inside?

local formattedCommands = {}
for _,com in pairs(commands) do

	-- Convert it into a string and place it to the table
	local comString = com.Value -- If it is a name then change it to com.Name

	comString = "/" .. comString -- Add the prefix
	
	table.insert(formattedCommands, comString) -- Placing it into the table
end

and use table.find() to check the command if it is on the table.

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if table.find(formattedCommands, msg) then
			print(msg)
		end
	end)
end)

I don’t get an error yet it still doesn’t print the message.

This also didn’t work. No error is given.

Oh wait oops uh

Yea why is commands a table of objects?
You store the commands in a, well, interesting way, so you have to convert it to a table of strings firsts. I am going to assume that the childrens of script.Parent.AdminSystem.Commands are stringValues and not some other random things, and that the commands are stored in their .Value and not as their name. In that case, simply use a for loop to iterate through the table and replace each object with its string value.

Jade’s first piece of code should work.

They aren’t string values yet module scripts.

1 Like

Module scripts?

Could you show me your explorer or explain more

Yeah, so within the commands folder are modules scripts, in the module script it contains the code of the command.

image

try this

local prefix = "/"
game:GetService("Players").PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if string.sub(msg, 1, 1) == prefix then
			local command = commands:FindFirstChild(string.split(string.sub(msg, 2, #msg), " ")[1])
			print(command)
		end
	end)
end)

edit: yes i editted this a few times i think i have not gotten enough rest

1 Like

Thank you so very much, been stuck on this for a great time.

oh and i guess you can also get arguments in this way:

local prefix = "/"
game:GetService("Players").PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if string.sub(msg, 1, 1) == prefix then
			local args = string.split(string.sub(msg, 2, #msg), " ")
			local command = commands:FindFirstChild(args[1])
			table.remove(args, 1)

			print(command, args)
		end
	end)
end)

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