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

for i = 1, #game:GetService("Players"):GetPlayers() do doesn't Seem to work?

I've been stuck on this for about 20 minutes and for some reason it doesn't run the same amount of times as there are players.

for i = 1, #game:GetService("Players"):GetPlayers() do
			local Sam = SERVERQBA:Clone()
			Sam.Name = i
			Sam.Parent = SERVERQBAT
			Sam.LayoutOrder = i
			SERVERQBAT.CanvasSize = UDim2.new(0, 0, 0, 50 * i)
		end

10 comments

0

Can we see the full script and if there are any errors? It is hard to help without much context.

0

I don't really see a need for that as I know 100% that's what is causing this. I replaced the #game:GetService("Players"):GetPlayers() with 10 and it repeated i 10 times. It's a local script in a gui, and the GUI is in StarterGui. Feel like thats a fair amount of information.

0

When does this script run? What is the loop doing? Sometimes the problem is not always right in front of you.

0

There you go, edited.

0

Have you tried printing the value of #game:GetService("Players"):GetPlayers() before the loop to find out the actual amount?

0

No, I'll do that rn

0

if #game:GetService("Players"):GetPlayers() equal to 0 this loop will not loop, that's why, there are peobarely no players i think

0

But starmaq if there is no players, then how he knows if that's working? He can only know by joining to the game..

0

The script might have ran before the player object was even added.

0

-1 Use a title that explains your question

2 answers

Use PlayerAdded:

game.Players.PlayerAdded:Connect(function()
	local plrs = game.Players:GetPlayers()
	for i = 1, #plrs do
		-- Rest of the code here
	end
end)

The for loop will never fail to detect at least 1 player if used in this manner.

I've had this problem multiple times before, and I found out that the script was running before a player was even added. The solution I did for this was I added it in a PlayerAdded event, but it makes it run every time a new player joins.

Probably use a repeating loop until the number of Players:GetPlayers() is more than 1

3 comments

0

Excuse me if I said anything wrong, I'm in a hurry

0

dont use a repeat loop like that

0

Right

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