Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
-2

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

Asked by
Avi_i 0
6 months ago
Edited 6 months 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 262 — 6mo
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 0 — 6mo
0
When does this script run? What is the loop doing? Sometimes the problem is not always right in front of you. GoldAngelInDisguise 262 — 6mo
0
There you go, edited. Avi_i 0 — 6mo
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 262 — 6mo
0
No, I'll do that rn Avi_i 0 — 6mo
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 834 — 6mo
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 364 — 6mo
0
The script might have ran before the player object was even added. GoldAngelInDisguise 262 — 6mo
0
-1 Use a title that explains your question kingdom5 5413 — 6mo

2 answers

Log in to vote
0
Answered by
BashCaster 2324 Moderation Voter Community Moderator
6 months 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 351 Moderation Voter
6 months 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 351 — 6mo
0
dont use a repeat loop like that User#23365 -5 — 6mo
0
Right blockmask 351 — 6mo

Answer this question