Player.chatted does nothing

So I have this script that is supposed to print “EEEEEEEEEEEEEEEEEEE” when you say “e” in chat (Yes I know it’s pretty stupid),
but it does nothing.

Here is the script:

local Players = game:GetService("Players")
--options
local option1 = "e"

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if message:lower() == option1:lower() then
		print("EEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
		end
	end)
end)

This script should work just fine if it is a regular script inside a service like ServerScriptService.

also this post should belong in #help-and-feedback:scripting-support

does this work?

local Players = game:GetService("Players")
--options
local option1 = "e"


for _,player in Players:GetPlayers() do
	player.Chatted:Connect(function(message)
		if message:lower() == option1:lower() then
			print("EEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
		end
	end)
end

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if message:lower() == option1:lower() then
			print("EEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
		end
	end)
end)

edit explanation:
so, the script didn’t work at first because it ran after the player joined the game
to fix this we loop through all players at the start

and a better way than my previous example would be using a function

for _,player in Players:GetPlayers() do
	playerAdded(player)
end

Players.PlayerAdded:Connect(playerAdded)

function playerAdded(player)
	player.Chatted:Connect(function(message)
		if message:lower() == option1:lower() then
			print("EEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
		end
	end)
end

Make sure to explain why this solution works. Also, you should package the following code into a function to reduce code duplication:

player.Chatted:Connect(function(message)
	if message:lower() == option1:lower() then
		print("EEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
	end
end)
1 Like

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