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

Problem with "for _,v in pairs(game.Players:GetChildren()) do"?

I have a problem, I make a minigame style of game. (Not minigames but round system, teleporting and that stuff) I want all player teleported but it's only teleporting one player. To see what was the problem I made the test found down below. I then found out that it was a problem with the "for _,v in pairs(game.Players:GetChildren()) do" How do I fix this?

I made a simple gui Looking like this: https://gyazo.com/3dcad28da3dd2002c725c16e8883f546

In the LocalScript you find this:

script.Parent.MouseButton1Click:connect(function()
    for _,v in pairs(game.Players:GetChildren()) do
        local Players = {}
        table.insert(Players, v.Name)
        local texfortexlabel
        texfortexlabel = table.concat(Players, ", ") .. " are the current players in the game!"
        script.Parent.Parent.TextLabel.Text = texfortexlabel
        print(texfortexlabel)
    end
end)

I made a 2 player server and this is what happens: https://gyazo.com/5c702263255955bb416a26a237afce53 There are 2 players in the game but it says there are one.

I made a 4 player server and this is what happens: https://gyazo.com/79134f4836cecfa78e3d13644ec95a42 Again it says that it only is 1 player in the game. Why does it say that?

3 answers

✓ Accepted answer
local Players = {}
script.Parent.MouseButton1Click:connect(function()
	    for _,v in pairs(game.Players:GetChildren()) do
	        table.insert(Players, v.Name)
	        local texfortexlabel
	        texfortexlabel = table.concat(Players, ", ") .. " are the current players in the game!"
		script.Parent.Parent.TextLabel.Text = texfortexlabel        
		print(texfortexlabel)
    end
end)

1 comment

0

Mind providing an explanation for your answer so the asker has a better understanding?

If you want a Text label to say the amount of players there are, do this:

-- Localscript under text label.	
local players = game:GetService'Players'
local prop = 'NumPlayers'

players:GetPropertyChangedSignal(prop):Connect(function()
    script.Parent.Text = 'There are '..players.NumPlayers..' in the server'
end)
script.Parent.MouseButton1Click:connect(function()
        script.Parent.Parent.TextLabel.Text = "There are "..#game.Players:GetPlayers().." in the game!"
end)

Hope this helps!

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