Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to get every play instead of using Players.PlayerAdded?

Asked by 3 years ago

Hey I'm just wondering if there's a way to get every player at once, like .PlayerAdded?

I was told

game.Players:GetPlayers()

would work, but I'm trying to use player.Chatted, and it didn't work,

local players = game.Players:GetPlayers()
players.Chatted:Connect(function(msg)
    print(msg)
end)

I've tried inserting every player into a table, but that didn't work, Thanks for reading this, answers are extremely appreciated :)

1 answer

Log in to vote
1
Answered by
Hydrogyn 155
3 years ago
Edited 3 years ago

We can loop through the players and connect the chatted event to every player.

for Index,Value in pairs(game:GetService("Players"):GetPlayers()) do
    value.Chatted:connect(function(msg)
        print(msg)
    end)
end

hydrogyn

EDIT:

the solution to the problem in the comments.

local Players = game:GetService("Players")
local PlayersInGame = {} -- must contain only objs

function UpdateFunction(playerobj)
    playerobj.Chatted:connect(function(msg)
        print(msg)
    end)
end

function AddPlayerToTable(playerobj)
    table.insert(PlayersInGame,playerobj)
    UpdateFunction(playerobj)
end
Players.PlayerAdded:connect(AddPlayerToTable)

function RemovePlayerFromTable(playerobj)
    local I = table.find(PlayersInGame,playerobj)
    table.remove(PlayersInGame,I)
    UpdateFunction(playerobj)
end
Players.PlayerRemoving:connect(RemovePlayerFromTable)

I tested it and it works. :)

hydrogyn

0
^yes but be careful. Once this runs then players that join in the game after will not have the chat event work. The way you had it works the best. But this does solve ur problem :) Heavenlyblobmaster 271 — 3y
0
Thanks! EllaTheFloofyFox 106 — 3y
Ad

Answer this question