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?
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
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)"
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
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.
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
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.
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?