I’m making a script to detect when 2 players join a game, and when this happens, a GUI frame will change text. However, it doesn’t seem to happen to all players in the for i,v loop.
local function playerAdded(player)
for i,v in ipairs(game.Players:GetChildren()) do
player = v
coroutine.wrap(Tell)(player)
end
end
local function playerRemoved(player)
for i,v in ipairs(game.Players:GetChildren()) do
player = v
coroutine.wrap(Tell)(player)
end
end
function Tell(player)
local Players = game.Players:GetChildren()
print(#Players)
if #Players >= 2 then
print("Hi")
game.StarterGui.ScreenGui.Frame.TextLabel.Text = "Ready to play!"
end
if #Players < 2 then
game.StarterGui.ScreenGui.Frame.TextLabel.Text = "Not enough players!"
end
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoved)
What’s interesting is in the output, the message “Hi” that I want it to print does print twice, which means it does acknowledge that it needs to run twice for both players, but the text doesn’t react that way. Anyone know why?
I put that to nonlocal because the other functions would not recognize it. Also, your last statement, then how come the main nonlocal function ran numerous times?
function Tell(player)
local Players = game.Players:GetChildren()
print(#Players)
local textLabel = player:WaitForChild('PlayerGui'):WaitForChild('ScreenGui').Frame.TextLabel
if #Players >= 2 then
print("Hi")
textLabel.Text = "Ready to play!"
else
textLabel.Text = "Not enough players!"
end
end