Message detector not working correctly

Hello developers! So, I’m currently working on a race flag system, which detects if player chatted a message that is a StringValue inside of ReplicatedStorage. More precisely, there is a folder with 5 values inside of ReplicatedStorage. All of these values are the chat messages that if player chats, a function happens.

Let’s say I chatted “Red Flag”. It is only supposed to print “red flag”, but it prints all of them (besides green for some reason).
image

My code:

local GreenFlag = game.ReplicatedStorage.RaceControlSystem.Values.DEP_GF.Value
local YellowFlag = game.ReplicatedStorage.RaceControlSystem.Values.DEP_YF.Value
local RedFlag = game.ReplicatedStorage.RaceControlSystem.Values.DEP_RF.Value
local VirtualSafetyCar = game.ReplicatedStorage.RaceControlSystem.Values.DEP_VSC.Value
local SafetyCar = game.ReplicatedStorage.RaceControlSystem.Values.DEP_SC.Value

local PlayersService = game:GetService("Players")

-- Green Flag
PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.find(string.lower(Message), string.lower(GreenFlag)) then
			print "gren flag"
		end;
	end);
end);

-- Yellow Flag
PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.find(string.lower(Message), string.lower(YellowFlag)) then
			print "yellow flag"
		end;
	end);
end);

-- Red Flag
PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.find(string.lower(Message), string.lower(RedFlag)) then
			print "red flag"
		end;
	end);
end);

-- VSC
PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.find(string.lower(Message), string.lower(VirtualSafetyCar)) then
			print "vsc"
		end;
	end);
end);

-- SC
PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.find(string.lower(Message), string.lower(SafetyCar)) then
			print "sc"
		end;
	end);
end);

If something isn’t clear, please let me know.

You are making a new playeradded and chat connection for every player. You just need to use one and utilize elseif statement


PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.lower(Message) == "yellow flag" then
			print("yellow flag")
elseif string.lower(Message) == "red flag" then
print("red flag")
-- basically repeat this process
		end;
	end);
end);

Sorry for bad formating. Currently on mobile! Also there may be typos

1 Like

Thank you for the fast response.

Your code generally works, however when I say for example “Red_Flag”, the print is “green flag”. And it’s like that with other messages too.

I changed the code, so that might be because of that, but I don’t really think so.
By the way I had to change it because your code detected a straight and set message, but mine is meant to detect a message from a value (kind of).

local GreenFlag = game.ReplicatedStorage.RaceControlSystem.Values.DEP_GF.Value
local YellowFlag = game.ReplicatedStorage.RaceControlSystem.Values.DEP_YF.Value
local RedFlag = game.ReplicatedStorage.RaceControlSystem.Values.DEP_RF.Value
local VirtualSafetyCar = game.ReplicatedStorage.RaceControlSystem.Values.DEP_VSC.Value
local SafetyCar = game.ReplicatedStorage.RaceControlSystem.Values.DEP_SC.Value

local PlayersService = game:GetService("Players")

PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.lower(GreenFlag) then
			print("green flag")
		elseif string.lower(YellowFlag) then
			print("yellow flag")
		elseif string.lower(RedFlag) then
			print("red flag")
		elseif string.lower(VirtualSafetyCar) then
			print("vsc")
		elseif string.lower(SafetyCar) then
			print("sc")
		end
	end)
end)

I’ve tried this:

local GreenFlag = game.ReplicatedStorage.RaceControlSystem.Values.DEP_GF.Value
local YellowFlag = game.ReplicatedStorage.RaceControlSystem.Values.DEP_YF.Value
local RedFlag = game.ReplicatedStorage.RaceControlSystem.Values.DEP_RF.Value
local VirtualSafetyCar = game.ReplicatedStorage.RaceControlSystem.Values.DEP_VSC.Value
local SafetyCar = game.ReplicatedStorage.RaceControlSystem.Values.DEP_SC.Value

local PlayersService = game:GetService("Players")

PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.lower(GreenFlag) then
			script.Parent.BrickColor = BrickColor.new("Lime green")
		elseif string.lower(YellowFlag) then
			script.Parent.BrickColor = BrickColor.new("Bright yellow")
		elseif string.lower(RedFlag) then
			script.Parent.BrickColor = BrickColor.new("Bright red")
		elseif string.lower(VirtualSafetyCar) then
			script.Parent.BrickColor = BrickColor.new("Neon orange")
		elseif string.lower(SafetyCar) then
			script.Parent.BrickColor = BrickColor.new("Pastel yellow")
		end
	end)
end)

But the brick end up being Lime Green everytime. So when I say “Red_Flag”, it becomes green but it is supposed to be Bright Red.

EDIT:
Apparently, the problem is changing value into a text. I’ve tried setting the trigger message to a “straight” message, and it worked perfectly fine.

So now I just need to change a StringValue into Text. I don’t know how tho. If it was a GUI, I think that’d just be […].Text = […].Value, but no Idea what about messages.

You aren’t comparing the Message to the string.lower(GreenFlag), your code is just saying “if I can string.lower(GreenFlag) then we should do this” so it will never get to the next elseif.

edit: to clarify, its comparing it like if its a true or false statement. for example, if I had a function

function doIExist()
      if game.Workspace:FindFirstChild("hi") then
         return true
       else
         return false
     end
end 

and I was checking for that function, it would return true. A lot (maybe all) Roblox functions can be used to just “check” if it can be done, and if it can be done, it returns true.

I don’t really understand what do you mean…? Are you saying that the string.lower(GreenFlag) is not comparing to a message? Because the GreenFlag is a statement for an existing value (Green_Flag) which is the message. I just don’t really get what you are saying.

Yeah, its checking if the value exists, not what the message is. You are doing “can the value of greenflag be lowercased” not “is the message lowercase equal to greenflag value lowercase”

if string.lower(Message) == string.lower(GreenFlag) then
			script.Parent.BrickColor = BrickColor.new("Lime green")
...

Also because you are comparing it so much you should just make a function like:

function checkmsgval(message,value)
          if string.lower(message) == string.lower(value) then
                return true
              else
                return false
         end
end

-- then you can just do 

if checkmsgval(Message, GreenFlag) then
 -- do your code
elseif checkmsgval(Message, YellowFlag) then
...
```

Got it working. Thanks really much.
Basically just did this

PlayersService.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.find(string.lower(Message), string.lower(GreenFlag)) then
			print("GreenFlag")
		elseif string.find(string.lower(Message), string.lower(YellowFlag)) then
			print("YellowFlag")
		elseif string.find(string.lower(Message), string.lower(RedFlag)) then
			print("RedFlag")
		elseif string.find(string.lower(Message), string.lower(VirtualSafetyCar)) then
			print("VSC")
		elseif string.find(string.lower(Message), string.lower(SafetyCar)) then
			print("SC")
		end;
	end);
end);
1 Like