Can anyone show me why this isn't working?

Hi all, I’ve been trying to make a tool that can be toggled on and off with a click. When it’s considered “active”, it fires a remote event on the server, that proceeds to fire a remote event on all connected clients and tells them which player fired the remote event.

The listener script is supposed to detect whether the target player has chatted a specific message, however the .Chatted event is not firing:

local RS = game:GetService("ReplicatedStorage")
local RemoteEvent = RS.CursedSpeechFX.SpeechRemote

local toggleTable = {}

RemoteEvent.OnClientEvent:Connect(function(Player)
	if Player then
		local active = toggleTable[Player.UserId] or 1
		
		if active == 1 then
			toggleTable[Player.UserId] = 999
			
			print("speech on")
			
			Player.Chatted:Connect(function(msg)
				if msg == "explode" then
					print("exploding")
				end
			end)
			
			task.delay(1, function()
				toggleTable[Player.UserId] = 2
			end)
		elseif active == 2 then
			toggleTable[Player.UserId] = 999
			
			print("speech off")

			task.delay(1, function()
				toggleTable[Player.UserId] = 1
			end)
		elseif active == 999 then
			return
		end 
	end
end)

Does anyone know why this is the case? All help would be appreciated including alternate routes I could go about this. I’ve asked the AI Assistant for a bit and while it’s provided solutions for me in the past it hasn’t given me anything for this.

1 Like

I think it’s because you’re passing a player instance along the event - instances tend to come out the other end as nil when it’s a RemoteEvent. Try passing the name, and then doing Player = game:GetService("Players"):FindFirstChild(Player).

Creating a function every time the event is fired and never disconnecting it isn’t a good idea - it can lead to memory leaks. Try using :Once() instead of :Connect() (so it would be Player.Chatted:Once(function(msg) end)), which will disconnect the connection after it has been fired once.