For loop not working

It just won’t work! for some reason.
Here is my script

for i, v in pairs(game.Players:GetPlayers()) do
	local Notif = Instance.new("TextLabel", v.PlayerGui.OtherNotifHolder)
	local Gradient = script.Sample:Clone()
	Gradient.Parent = Notif
    -- there is more script here but here is main cause
end
-- the func it's wrapped in
wait()
start(true, 4) <--

capturerroragain

I recommend you using debug/breakpoints and print() (with Expressive Output Window beta enabled), so it will be easier to understand what’s going on…

1 Like

I already had it on, I don’t know why it won’t work.

Hey eb9h.

I would love to help with your script but I’m a little unclear on what the problem is right now. You said the for loop “won’t work” what is it doing that you don’t want it to do, or what is it not doing that you want it to do? Please me more clear when asking for help.

To better help you, you need to say what is not working, what is your goal with this code.

1 Like

It’s a function that creates a TextLabel in all of the players.PlayerGui.OtherNotifHolder

function start(count, for_)
	if count == true then
		for i, v in pairs(game.Players:GetPlayers()) do
			local Notif = Instance.new("TextLabel", v.PlayerGui.OtherNotifHolder)
			local Gradient = script.Sample:Clone()
			Gradient.Parent = Notif
			Notif.TextScaled = true
			Notif.AnchorPoint = Vector2.new(0.5, 0.5)
		end
	end
end
wait()
start(true, 4)

Are you by any chance putting this in a LocalScript?

This still does not specify how the for loop is not behaving how a for loop should. Is it not doing that?

How should I write my topic?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
  2. What is the issue? Include screenshots / videos if possible!
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub ?
1 Like

No. I put it in a regular Script, inside of ServerScriptService. Should I put the script somewhere else?

No, but as I and @DylWithlt said, you need to be more specific, you didn’t tell us exactly what is not working…

  1. What do you want to achieve? Keep it simple and clear!
    I want to put a TextLabel in all of the current players in-game with a ScreenGui.
  2. What is the issue? Include screenshots / videos if possible!
    capturerroragain
    captureerroragainagain
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub ?
    I have tried using :WaitForChild() and :FindFirstChild(). They both give no errors and don’t work.
    As for the Developer Hub, I have searched.

You just showed images you didn’t explain the problem at all. This doesn’t tell us ANYTHING.

I assume he wants to make an admin GUI, and he needs this loop so he can select a gui with a player’s name and kick them.

From what little I was able to understand, there is an important question that you must understand (something that I also had doubts in the beginning):
Roblox has Server and Local concepts:

If you use any ScreenGui elements, you must do so in LocalScript because this is the script that runs on the player’s machine (Client);
This way, I don’t think it is necessary to scan a list of players, but you can do this by simply entering a code at the beginning of LocalScript, which will be executed for each player, each time the player enters the game.
It is also important to understand that every time a player enters, a ScreenGui is automatically replicated to game.Players, so ideally, you should NOT interact directly with game.StarterGui, but with everything that is replicated within game.Players.LocalPlayer.

local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Gui = PlayerGui:WaitForChild("ScreenGui")

Alright, let me give you some code to try. One thing you’re doing in your code is setting the parent of Notif “Text Label” instance with the second argument. I believe this practice was deprecated instead opting for Notif.Parent = v.PlayerGui.OtherNotifHolder. One thing you can try is checking if it even exists before parenting it to make sure the loop is finding the correct object to parent to.

function start(count, for_)
	if count == true then
		for i, v in pairs(game.Players:GetPlayers()) do
			local onh = v.PlayerGui:FindFirstChild("OtherNotifHolder")
			if not onh then warn("OtherNotifHolder not found for", v) continue end
			
			local Notif = Instance.new("TextLabel")
			local Gradient = script.Sample:Clone()
			Gradient.Parent = Notif
			Notif.TextScaled = true
			Notif.AnchorPoint = Vector2.new(0.5, 0.5)
			Notif.Parent = onh
		end
	end
end
wait()
start(true, 4)

Let me know if this works.

You can actually access each clients PlayerGui via server scripts now. It is PlayerReplicated but its no longer ClientOnly.

1 Like

I am not a super proficient script myself, but could it be the Gradient.Parent = Notif? Just a random thought

I tried this script, basically what you said:

function start(count, for_)
	if count == true then
		for i, v in pairs(game.Players:GetPlayers()) do
			local onh = v.PlayerGui:FindFirstChild("OtherNotifHolder")
			
			if not onh then
				warn("OtherNotifHolder could not be found in", v)
				continue
			end
			local Notif = Instance.new("TextLabel")
			local Gradient = script.Sample:Clone()
			Gradient.Parent = Notif
			Notif.TextScaled = true
			Notif.Parent = onh
			Notif.AnchorPoint = Vector2.new(0.5, 0.5)
			Notif.Font = Enum.Font.AmaticSC
			Notif.TextColor3 = Color3.new(255, 255, 255)
			Notif.Size = UDim2.new(1, 0, 0.084, 0)
			Notif.Position = UDim2.new(0, 479, 0, 25)
			Notif.TextColor3 = Color3.fromRGB(255, 255, 255)
			Notif.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
		end
	end
end

Gave no warnings, but still, nothing pops up on my screen’
I tried removing the functions count and for_, plus the if loop.

If you run this right away, there may be no players in the game yet, so there will be nothing to loop through. Use PlayerAdded in your code in addition to the loop to catch those who join the game.

2 Likes