'for i,v in pairs' not running

In the beginning of this function i am trying to make a gui visible for everyone. It does nut work and ‘a’ is printed but not ‘b’.

local spins = 50

local function playerPick()
	print("a")
	for i,v in pairs(plrs:GetPlayers()) do
		print("b")
		v.PlayerGui:WaitForChild("PlayerPick").Frame.Visible = true
	end
	
	for number,plr in pairs(plrs:GetPlayers()) do
		if plr.IsAlive.Value == true and spins ~= 0 then
			wait(0.05)
			local avatarImage = plrs:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
			plr.PlayerGui.PlayerPick.Frame.ImageLabel.Image = avatarImage
			plr.PlayerGui.PlayerPick.Frame:FindFirstChild("Name").Text = plr.Name
			spins -= 1
			game.SoundService.SFX.ButtonClick:Play()
			if number == #plrs:GetPlayers() and spins ~= 0 then
				playerPick()
			else if number == #plrs:GetPlayers() and spins == 0 then
					game.SoundService.SFX.GuesserChosen:Play()
				end
			end
		end
	end
end

playerPick()

plrs” is not a variable. Add local plrs = game:GetService("Players") at the top at and it should work. And also, I do not recommend you getting the players this way. You could do something like:

local player
game:GetService("Players").PlayerAdded:Connect(function(plr)
    player = plr
end)

local function playerPick(plr)
    player.PlayerGui:WaitForChild("PlayerPick").Frame.Visible = true
end

playerPick(player)

The script is pretty big so i only added the part i needed help with here, plrs is actually a variable.Do you have any other idea of what might be the problem?

could you show that variable too?

local plrs = game:GetService("Players")

ok instead of printing ‘a’ can you try printing plrs:GetPlayers(), because i think the issue is that the players havent yet loaded, so getPlayers returns empty, causing the loop to not run, im not sure tho try printing it and see what happens

I put the function higher up in the script and called the function when another function ended and it worked.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.