Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-2

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

Asked by
Avi_i 2
5 years ago
Edited 5 years ago

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
0
Can we see the full script and if there are any errors? It is hard to help without much context. GoldAngelInDisguise 297 — 5y
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. Avi_i 2 — 5y
0
When does this script run? What is the loop doing? Sometimes the problem is not always right in front of you. GoldAngelInDisguise 297 — 5y
0
There you go, edited. Avi_i 2 — 5y
View all comments (6 more)
0
Have you tried printing the value of #game:GetService("Players"):GetPlayers() before the loop to find out the actual amount? GoldAngelInDisguise 297 — 5y
0
No, I'll do that rn Avi_i 2 — 5y
0
if #game:GetService("Players"):GetPlayers() equal to 0 this loop will not loop, that's why, there are peobarely no players i think starmaq 1290 — 5y
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.. Miniller 562 — 5y
0
The script might have ran before the player object was even added. GoldAngelInDisguise 297 — 5y
0
-1 Use a title that explains your question User#5423 17 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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.

Ad
Log in to vote
-1
Answered by
blockmask 374 Moderation Voter
5 years ago

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

0
Excuse me if I said anything wrong, I'm in a hurry blockmask 374 — 5y
0
dont use a repeat loop like that User#23365 30 — 5y
0
Right blockmask 374 — 5y

Answer this question