I recently implemented an Auto-Update feature for a script that I've been developing. There are 14 functions that rely on the player.Chatted event in the script and they function as expected until I combine them with the update method.
Working code before being cloned and updated:
game.Players.PlayerAdded:connect(function(player)
player.Chatted:connect(function(message) onChatted(message, player) end)
end)
function onChatted(message, player)
if message == "/Mute" then
MuteAll()
elseif message == "/UnMute" then
UnMute()
elseif message == "/ClearStorageBin" then
ClearStorageBin()
elseif message == "/StopRain" then
StopRain()
end
end
game.Players.PlayerAdded:connect(function(player)
player.Chatted:connect(function(message)
onChatted(player, message)
end)
end)
The method of updating I'm using is the InsertService to get the newest version of the script and then calling :Destroy() on the outdated script.
The problem arises when the chat functions no longer trigger like they did. My best guess is that they would still work for any player who joins after the first player because the new script will have had time to listen for them using: game.Players.PlayerAdded:connect(function(player) whereas the first player was noticed by the first script but that script has since been destroyed and now the first player is not recognized as having joined.
I have also tried:
for _,player in pairs(Players:GetPlayers()) do
player.Chatted:connect(function(message)
onChatted(player, message)
end)
end
and:
for _, player in next, game.Players:GetPlayers() do
player.Chatted:connect(function(message) onChatted(player, message)
end)
end
in conjunction with the game.Players.PlayerAdded:connect(function(player)bit.
Is there a workaround to make sure that the first player can use the chat commands as well? Applying your ideas to the specific references above would be immensely helpful for me as well.
Thanks!

1 comment
Any players added after the code is run won't be affected.