Issue with player.Chatted not working

local HttpService = game:GetService("HttpService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local boardID = "redacted"
local whitelistLID = "redacted"
local trelloapikey = "redacted"
local whitelist = {
	--"Alex_Google214"
}
local wlb = {}
for _,v in ipairs(whitelist) do
	table.insert(wlb,v)
end

--if RunService:IsStudio() then
--	return "Not checking whitelist, player is in Studio"
--end

kickReasons = {}
kickReasons["notWhitelisted"] = "\n👋\n\n[ENG] Not Whitelisted. Goodbye.\n---\n[RU] Не в белом списке. До свидания.\n---\n[AZ] Ağ siyahıda deyilsiniz. Sağolun.\n---\n[DE] Sie sind nicht auf der Whitelist. Auf Wiedersehen."
kickReasons["kicked"] = "\n👋\n\n[ENG] You just got kicked. Goodbye.\n---\n[RU] Вас выгнали. До свидания.\n---\n[AZ] Siz indicə serverdən qovuldunuz. Sağolun.\n---\n[DE] Sie wurden gerade vom Server geworfen. Auf Wiedersehen."
kickReasons["be gone"] = "be gone"


local function lowercaseAllLists()
	-- whitelist
	local temp = {}
	for i,v in ipairs(whitelist) do
		table.insert(temp, string.lower(v))
	end
	whitelist = {}
	for i,v in ipairs(temp) do
		table.insert(whitelist, string.lower(v))
	end
	--wlb
	temp = {}
	for i,v in ipairs(wlb) do
		table.insert(temp, string.lower(v))
	end
	wlb = {}
	for i,v in ipairs(temp) do
		table.insert(wlb, string.lower(v))
	end
	temp = {}
end
local function refreshWhitelist()
	whitelist = {}
	-- manual whitelist copy
	for _,v in ipairs(wlb) do
		table.insert(whitelist,v)
	end
	
	-- trello whitelist
	local data
	local success, response = pcall(function()
		local response = HttpService:GetAsync("https://api.trello.com/1/lists/"..whitelistLID.."/cards?key="..trelloapikey)
		data = HttpService:JSONDecode(response)
	end)
	if not success then
		warn("Error: "..response)
	end

	for _,card in ipairs(data) do
		table.insert(whitelist,card.name)
	end
	lowercaseAllLists()
	print("Refreshed whitelists.")
end

refreshWhitelist()


game:GetService("Players").PlayerAdded:Connect(function(player)
	if not table.find(whitelist,string.lower(player.Name)) then
		player:Kick(kickReasons["notWhitelisted"])
	end
	player.Chatted:Connect(function(message)
		if player.Name == "Alex_Google214" then
			if string.sub(string.lower(message),1,4) == "/rwl" or string.sub(string.lower(message),1,18) == "/refresh whitelist" or string.sub(string.lower(message),1,17) == "/reload whitelist" then
				refreshWhitelist()
			elseif string.sub(string.lower(message),1,8) == "bе gone" or string.sub(string.lower(message),1,13) == "/refresh user" then	-- be gone - the "e" in "be" is russian E
				for i,plr in ipairs(game:GetService("Players"):GetChildren()) do
					if not table.find(whitelist,string.lower(plr.Name)) then
						if string.sub(string.lower(message),1,7) == "bе gone" then
							plr:Kick(kickReasons["be gone"])
						else 
							plr:Kick(kickReasons["notWhitelisted"])
						end
					end
				end
			elseif string.sub(string.lower(message),1,11) == "/e kicktool" or string.sub(string.lower(message),1,5) == "/e kt" or string.sub(string.lower(message),1,3) == "/kt" then
				local Alex = Players:FindFirstChild("Alex_Google214")
				if not Alex then return end
				local Tool = Instance.new("Tool", Alex.Backpack)
				Tool.Name = "Hammer"
				local a = Tool.Activated:Connect(function(mouse)
					if not mouse.Hit then return end
					local target = mouse.Hit:FindFirstAncestorOfClass("Model")
					if not target or not target:FindFirstChild("Humanoid") then return end
					if not Players:GetPlayerFromCharacter(target) then return end

					Players:GetPlayerFromCharacter(target):Kick(kickReasons["kicked"])
				end)
				Tool:Destroy()
			end
		end
	end)
end)

I have two functions; their main task is to refresh the whitelist when I run the command /rwl or /reload whitelist. The problem is that player.Chatted is not working sometimes. I tried putting print(message) in

player.Chatted:Connect(function(message)
-- here
end)

^ Still didn’t work. I didn’t put this in bug reports because I am not 100% sure that there’s no bug in the code. Help will be appreciated.

1 Like

I’ve looked at this code and it looks like it’s working! Tell me, are you still experiencing this problem? I will be glad if you give more information.

1 Like

Hmmmmmm.

Is this in a server script or a local script?

1 Like

Hello. The code provided above is in a server script

Hello. Yes, I am still experiencing the problem. It is pretty random if it will run the entire code or not. I think the functions on the top of the script are “clogging” the script or something - that is why the script does not execute till the end (especially the player.Chatted part).

I understand your problem and may have found a solution. Because your refreshWhitelist() function suspends the main thread, the player join event has no time to initialize and the server cannot commit a new player at that time. I slightly changed your code and added a system for waiting for new players

Full code:

local HttpService = game:GetService("HttpService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local boardID = "redacted"
local whitelistLID = "redacted"
local trelloapikey = "redacted"
local whitelist = {
	--"Alex_Google214"
}
local wlb = {}
for _,v in ipairs(whitelist) do
	table.insert(wlb,v)
end

-- variable to check waiting for list update. False state default
local WhitelistUpdated = false

--if RunService:IsStudio() then
--	return "Not checking whitelist, player is in Studio"
--end

kickReasons = {}
kickReasons["notWhitelisted"] = "\n👋\n\n[ENG] Not Whitelisted. Goodbye.\n---\n[RU] Не в белом списке. До свидания.\n---\n[AZ] Ağ siyahıda deyilsiniz. Sağolun.\n---\n[DE] Sie sind nicht auf der Whitelist. Auf Wiedersehen."
kickReasons["kicked"] = "\n👋\n\n[ENG] You just got kicked. Goodbye.\n---\n[RU] Вас выгнали. До свидания.\n---\n[AZ] Siz indicə serverdən qovuldunuz. Sağolun.\n---\n[DE] Sie wurden gerade vom Server geworfen. Auf Wiedersehen."
kickReasons["be gone"] = "be gone"


local function lowercaseAllLists()
	-- whitelist
	local temp = {}
	for i,v in ipairs(whitelist) do
		table.insert(temp, string.lower(v))
	end
	whitelist = {}
	for i,v in ipairs(temp) do
		table.insert(whitelist, string.lower(v))
	end
	--wlb
	temp = {}
	for i,v in ipairs(wlb) do
		table.insert(temp, string.lower(v))
	end
	wlb = {}
	for i,v in ipairs(temp) do
		table.insert(wlb, string.lower(v))
	end
	temp = {}
end
local function refreshWhitelist()
	
	-- During the update, new players will have to wait until the update is complete
	WhitelistUpdated = false
	
	whitelist = {}
	-- manual whitelist copy
	for _,v in ipairs(wlb) do
		table.insert(whitelist,v)
	end

	-- trello whitelist
	local data
	local success, response = pcall(function()
		local response = HttpService:GetAsync("https://api.trello.com/1/lists/"..whitelistLID.."/cards?key="..trelloapikey)
		data = HttpService:JSONDecode(response)
	end)
	if not success then
		warn("Error: "..response)
	end

	for _,card in ipairs(data) do
		table.insert(whitelist,card.name)
	end
	lowercaseAllLists()
	print("Refreshed whitelists.")
	
	-- Bringing everything back and giving players access
	WhitelistUpdated = true
	
end

function waitNewWhitelistUpdatedState(newState)
	if WhitelistUpdated == newState then return end -- Don't wait 0.1 seconds just like that
	repeat 
		task.wait(.1)
	until WhitelistUpdated == newState
end


--[[ 
	Creating a coroutine to instantly initialize
	the player join event without delaying the function "refreshWhitelist()"
]]
coroutine.resume(coroutine.create(function()
	refreshWhitelist()
end))

game:GetService("Players").PlayerAdded:Connect(function(player)
	
	-- The player will wait until the list is completely updated
	waitNewWhitelistUpdatedState(true)
	
	if not table.find(whitelist,string.lower(player.Name)) then
		player:Kick(kickReasons["notWhitelisted"])
	end
	player.Chatted:Connect(function(message)
		if player.Name == "Alex_Google214" then
			if string.sub(string.lower(message),1,4) == "/rwl" or string.sub(string.lower(message),1,18) == "/refresh whitelist" or string.sub(string.lower(message),1,17) == "/reload whitelist" then
				refreshWhitelist()
			elseif string.sub(string.lower(message),1,8) == "bе gone" or string.sub(string.lower(message),1,13) == "/refresh user" then	-- be gone - the "e" in "be" is russian E
				for i,plr in ipairs(game:GetService("Players"):GetChildren()) do
					if not table.find(whitelist,string.lower(plr.Name)) then
						if string.sub(string.lower(message),1,7) == "bе gone" then
							plr:Kick(kickReasons["be gone"])
						else 
							plr:Kick(kickReasons["notWhitelisted"])
						end
					end
				end
			elseif string.sub(string.lower(message),1,11) == "/e kicktool" or string.sub(string.lower(message),1,5) == "/e kt" or string.sub(string.lower(message),1,3) == "/kt" then
				local Alex = Players:FindFirstChild("Alex_Google214")
				if not Alex then return end
				local Tool = Instance.new("Tool", Alex.Backpack)
				Tool.Name = "Hammer"
				local a = Tool.Activated:Connect(function(mouse)
					if not mouse.Hit then return end
					local target = mouse.Hit:FindFirstAncestorOfClass("Model")
					if not target or not target:FindFirstChild("Humanoid") then return end
					if not Players:GetPlayerFromCharacter(target) then return end

					Players:GetPlayerFromCharacter(target):Kick(kickReasons["kicked"])
				end)
				Tool:Destroy()
			end
		end
	end)
end)

Parts of the code have been added such as:

-- variable to check waiting for list update. False state default
local WhitelistUpdated = false

Added to the top of the script


function waitNewWhitelistUpdatedState(newState)
	if WhitelistUpdated == newState then return end -- Don't wait 0.1 seconds just like that
	repeat 
		task.wait(.1)
	until WhitelistUpdated == newState
end

The function of waiting for the desired state of the variable


--[[ 
	Creating a coroutine to instantly initialize
	the player join event without delaying the function "refreshWhitelist()"
]]
coroutine.resume(coroutine.create(function()
	refreshWhitelist()
end))

-- During the update, new players will have to wait until the update is complete
WhitelistUpdated = false

and

-- Bringing everything back and giving players access
WhitelistUpdated = true

Added to refreshWhitelist() function


-- The player will wait until the list is completely updated
waitNewWhitelistUpdatedState(true)

Added to player join event.

Hope this solution helps you. Let me know if you have any questions or additional problems related to this topic.

1 Like

try this for the if :

message:find(your_word) or message:lower():find(your_word:lower()) or message:upper():find(your_word:upper())

also this is wrong :

table.find(whitelist,string.lower(player.Name))

it should be

table.find(whitelist,string.lower(string.lower(player.Name)))