Script Won't Continue After "if game.Players.LocalPlayer" Argument

Hello! I’ve had this issue for a while now and I can’t seem to fix it. Basically, I have a code that parents a LocalScript to the PlayerGui, and another that detects the LocalPlayer.

local LocalPlayer = game:GetService("Players"):WaitForChild("pqranoiddboy", 15)

if LocalPlayer then
	print("LocalPlayer found!")
end

-- STUFF UNDER HERE DOESN'T WORK --

local SizeColorScript = script.Size_Color

game:GetService("Players").PlayerAdded:Connect(function(player)
	for i, player in pairs(game:GetService("Players"):GetChildren()) do
	local PlayerGui = player:WaitForChild("PlayerGui")
		if PlayerGui then
			if not PlayerGui:FindFirstChild(SizeColorScript.Name) then
			SizeColorScript:Clone().Parent = PlayerGui
			end
		end
	end
end)

Nothing after the “-- STUFF UNDER HERE DOESN’T WORK --” works for some reason. When I remove everything above the “UNDER HERE” line, the script under it starts working again.

The console doesn’t give me any errors too and successfully prints the “LocalPlayer Found” too. How do I fix this?

1 Like

Sometimes in studio while playing solo, the PlayerAdded event doesn’t fire when your character comes in. I had that happen sometimes when testing stuff in Studio.

I usually lay it out something like this:

function playeradded()
	for i, player in pairs(game:GetService("Players"):GetChildren()) do
		local PlayerGui = player:WaitForChild("PlayerGui")
			if PlayerGui then
				if not PlayerGui:FindFirstChild(SizeColorScript.Name) then
					SizeColorScript:Clone().Parent = PlayerGui
				end
		end
end

game:GetService("Players").PlayerAdded:Connect(playeradded)

for i,v in pairs(game:GetService("Players"):GetPlayers()) do
	playeradded()
end
2 Likes

Oh don’t worry about that, the PlayerAdded thing does work when I remove everything above the line.

Interesting. is this code written in a ServerScript (aka Script) or a LocalScript? Only LocalScripts are able to utilize game.Players.LocalPlayer.

It’s in a ServerScript, but I don’t use LocalPlayer. The variable is called LocalPlayer but it’s not
"game.Players.LocalPlayer",
it’s
"game:GetService("Players"):WaitForChild("pqranoiddboy", 15)"

I’ve never used “return” and don’t know how it works, but could it be related to that?

So I was just testing it in studio, and when I take these lines out:

local LocalPlayer = game:GetService("Players"):WaitForChild("pqranoiddboy", 15)

if LocalPlayer then
	print("LocalPlayer found!")
end

Everything seems to work as intended.

It does work (except for the part under the line) and there are no errors or warnings, but I’ve experienced this before where something doesn’t work after I use a while true do argument or if argument.

why don’t you just look for yourself under playeradded?

game.Players.PlayerAdded:Connect(function(player)
if player.Name == “pqranoiddboy” then
end
end)

the reason the stuff under doesn’t do anything is because the game is waiting for the child and what is under it won’t run as it is halted on a wait. basically it isn’t listening for PlayerAdded while it’s still listening for WaitForChild

That means that it won’t work even after finding what the WaitForChild was looking for?

It means the script is halted or paused. While it’s waiting on WaitForChild. the script is currently waiting on that therefore the rest of the script isn’t listening to anything going on. When a player gets added it would fire normally but you are waiting for the player at the top of the script before anything else is done in the script so it will wait on that before anything else.

Meaning that it’s better to either use PlayerAdded or use WaitForChild at the bottom of the script, right?

WaitForChild delays the time it takes for the script to reach the PlayerAdded connection so it doesn’t fire for the player in question. You need to account for existing players in your server as well. Typically, in either production or testing code, you should always be using PlayerAdded as follows:

local Players = game:GetService("Players")

local function playerAdded(player)
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end
1 Like

It would be better to wrap waitforchild in a function instead of the script, but in this case PlayedAdded does what you want it to do. Even at the bottom of the script it would still not listen for PlayerAdded because that is a function that is called on player joining so it wouldn’t even be read. It would basically skip past it and read WaitForChild again waiting on it and not listening for the playeradded function.

1 Like

In this situation, waiting for the script to reach PlayerAdded isn’t that much of a problem, but I’ve been waiting for over 2 minutes and it didn’t do anything although the LocalPlayer was found pretty quickly. What do I do? Is that normal?

Nevermind, my PlayerAdded function was working but had a problem that I hadn’t noticed, sorry!

1 Like

Two issues:

  1. If this is a serverscript, localplayer does not exist.

  2. If this is a localscript, the players are created before the localscript, which means the .PlayerAdded event won’t even run.

1 Like