I'm trying to make a server chat bot that anyone can use for help with the game. The problem is that it won't account for any players that join after it's ran, so it only works for the first person who joins. Can someone help me?
Here's my code:
local plrs = game.Players:GetPlayers()
for i = 1, #plrs do
plrs[i].Chatted:Connect(function(chat)
if chat:sub(1, 1) == prefix then
wait(0.2)
-- Global cmds
if chat:match(prefix.."help") then msg("filler")
elseif chat:match(prefix.."test") then msg("Success")
elseif chat:match(prefix.."send") then msg(string.sub(chat, 7))
-- Admin cmds below
elseif chat:match(prefix.."startmsg") then
if v.Name ~= ownername then
msg("Admin command only!")
elseif v.Name == ownername then
msg("Chat '-help' for server help!")
end
end
end
end)
end

4 comments
What if there's already people in the server?
That does not matter when the script first runs there should be no players in the server if you really want to make sure then just do for i,v in pairs(game.Players.GetPlayers()) do code here end)
So I have to put all of the code twice? One for playeradded and the other for that
no, just this script