Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

2 comments

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 :)

0

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.