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 12 days ago
Edited 12 days 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 238 — 12d
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. iYzarky 0 — 12d
0
When does this script run? What is the loop doing? Sometimes the problem is not always right in front of you. GoldAngelInDisguise 238 — 12d
0
There you go, edited. iYzarky 0 — 12d
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 238 — 12d
0
No, I'll do that rn iYzarky 0 — 12d
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 349 — 12d
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 336 — 12d
0
The script might have ran before the player object was even added. GoldAngelInDisguise 238 — 12d
0
-1 Use a title that explains your question kingdom5 5086 — 12d

2 answers

Log in to vote
0
Answered by 12 days 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 242 Moderation Voter
12 days 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 242 — 12d
0
dont use a repeat loop like that User#23365 0 — 12d
0
Right blockmask 242 — 11d

Answer this question