Command not working and nothing is put into output

Hey everyone, I’m trying to make a command but it doesn’t work. No errors in output whatsoever. Please help me, it’s really appreciated.
Code:

local groupId = 123456
local adminRank = 10

function onChatted(msg, speaker)
 if speaker:GetRankInGroup(groupId) >= adminRank then
	local source = string.lower(speaker.Name)
	msg = string.lower(msg)
		if msg == "!rules" then
			print("testing")
			else
		end
end
end
game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:connect(function(msg)onChatted(msg, player) end)
end)

Any help is greatly appreciated, thanks!

1 Like

Is this a local or server script?

It’s a server script. Though, I will have to change everyone’s camera position with it. Do you think that would be possible with a server script?

You can use a remote event to change everyone’s camera, use

RemoteEvent:FireAllClients()

Great, thank you. I’ll look into that, but my command isn’t working, do you know a fix to it?

Got this when tried that.
image
image

Hey, I don’t see anything wrong in the code. But prints are great for debugging, you should put prints after every if in the function, and see where it stops printing.
That’s what I usually do when I don’t see an error but something doesn’t work.

You can add a print before every if, after the OnChatted line, if it prints, you atleast know the function does run.

function onChatted(msg, speaker)
print("function runs")

If that’s printed, then you can move to the next one.

It says I’m not enough high rank, though I am rank 17 if I understood correctly.
image

local groupId = 123456
local adminRank = 9

function onChatted(msg, speaker)
	print("function runs")
	 if speaker:GetRankInGroup(groupId) >= adminRank then
		print("if speaker get rank if print here")
	local source = string.lower(speaker.Name)
	msg = string.lower(msg)
		if msg == "!rules" then
			print("testing")
			else
     print("else thing after priint testing")
		end
end
end
game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:connect(function(msg)onChatted(msg, player) end)
end)

Yes, I do have the group id set correctly but just changed it.

1 Like

So as you can see, it runs, but reaches the else thing, which means the message is not exactly !rules
So I think I know what your problem is, chat messages include a lot of spacing since the player name shows before them. So instead of using == , use string.find(“!rules”, msg), I am not sure if there’s a thing like replace, here’s the string library:

string

1 Like

Same stuff appears to have printed.
image

local groupId = 123456
local adminRank = 9

function onChatted(msg, speaker)
	print("function runs")
	 if speaker:GetRankInGroup(groupId) >= adminRank then
		print("if speaker get rank if print here")
	local source = string.lower(speaker.Name)
	msg = string.lower(msg)
		if msg == string.find("!rules", msg) then
			print("testing")
			else
print("else thing after priint testing")
		end
end
end
game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:connect(function(msg)onChatted(msg, player) end)
end)
1 Like

Oh, oops I didnt mean you’ll do msg == string.find, if you use the string.find, there’s no need for the msg ==

I am pretty sure string.find returns a Boolean, so there’s no need for the == , since the message is not equal to true or false, it’s a string.

Like this:

if string.find(“!rules”, msg) then

Oops, yeah Vong25 is right, I accidentally wrote them the opposite, my fault.

1 Like

Tried if string.find("!rules", msg) then
but still returned same prints.

Flip around your arguments; first argument should be the message, and the second should be “!rules.”

2 Likes