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 4 years ago

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

I was told

1game.Players:GetPlayers()

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

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

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
4 years ago
Edited 4 years ago

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

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

hydrogyn

EDIT:

the solution to the problem in the comments.

01local Players = game:GetService("Players")
02local PlayersInGame = {} -- must contain only objs
03 
04function UpdateFunction(playerobj)
05    playerobj.Chatted:connect(function(msg)
06        print(msg)
07    end)
08end
09 
10function AddPlayerToTable(playerobj)
11    table.insert(PlayersInGame,playerobj)
12    UpdateFunction(playerobj)
13end
14Players.PlayerAdded:connect(AddPlayerToTable)
15 
View all 21 lines...

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 — 4y
0
Thanks! EllaTheFloofyFox 106 — 4y
Ad

Answer this question