Updating Chatted function playerlist when someone joins

The problem with the following script is that it only runs once and if I add a game.Players.PlayerAdded:Connect(function(player) function to it, it will run multiple times when someone types hi.

Action = game.Players:GetPlayers()
for i = 1,#Action do
    Action[i].Chatted:connect(function(Message2)
    input = Message2:lower()
    if string.match(input, "hi") then
        print("Hello!")
    end
    end)
end
1 Like

Just put the script inside a while loop and you should be fine.

Could there be another method which is less strenuous on performance?

colbert made this post that might help you:

Loops are actually worse for this specific case scenario and I’m not too sure how what I posted is relevant to the question being asked. Please use what I post carefully.

In the case of needing to connect chat functions, you should be using an event-based system. I don’t know what the problem is in the OP or why PlayerAdded is supposedly problematic but that’s the proper way to be writing chat-based systems.

local Players = game:GetService("Players")

local function playerAdded(player)
    player.Chatted:Connect(function (message)
        if string.match(string.lower(message), "hi") then
            print("Hello!")
        end
    end)
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end
1 Like