Cant get playerGui in a local script inside StarterPlayerScripts

I am trying to bind keybinds for certain actions
this script works completely fine without the addition of PlayerGui

i dont know what causes it but the script never gets the PlayerGui and it doesnt give a warn with infinite yield possible on WaitForChild

You cant do the other action when the line

local PlayerGui = player:WaitForChild("PlayerGui")

exists

basically the script completely stops when it tries to get the PlayerGui.

local Uis = game:GetService("UserInputService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer

local PlayerGui = player:WaitForChild("PlayerGui")
local InventoryGui = PlayerGui.InventoryGui

player.CharacterAdded:Connect(function(character)
	
	local humanoid:Humanoid = character:WaitForChild("Humanoid")

	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://118410660664793"
	animation.Name = "ProneAnim"
	animation.Parent = humanoid

	local ProneAnimationTrack = humanoid.Animator:LoadAnimation(animation)
	ProneAnimationTrack.Looped = true

	
	local e1
	e1 = Uis.InputBegan:Connect(function(input, gameProcessed)
		if gameProcessed then return end

		if input.KeyCode == Enum.KeyCode.LeftControl then
			if not ProneAnimationTrack.IsPlaying then
				ProneAnimationTrack:Play()
				if humanoid then
					humanoid.WalkSpeed = 5
				end
			else
				ProneAnimationTrack:Stop()
				if humanoid then
					humanoid.WalkSpeed = 16
				end
			end
		elseif input.KeyCode == Enum.KeyCode.Tab then

			InventoryGui.Enabled = not InventoryGui.Enabled
		end
	end)

	local e2
	e2 = humanoid.Died:Connect(function()
		e1:Disconnect()
		e2:Disconnect()
	end)
end)

local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList,false)
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
1 Like

:WaitForChild() is not suitable here. Using the dot . appears to be the only way to reference the object, as per the documentation.

local PlayerGui = player.PlayerGui

PlayerGui is clearly a child of the Player, there’s nothing stopping you from using WaitForChild.
That aside, this issue seems quite weird, as when I try to recreate this it definetely returns the PlayerGui.
Perhaps you could try using a BindableEvent to tell a script under the InventoryGui to close itself instead.

Other issue: You don’t run the CharacterAdded function if the character is already added before loading the script, which tends to happen quite a lot. You should run the CharacterAdded function before starting the :Connect once.

1 Like

The “other issue” could very likely also make you think that your issue is what it is, being the actual culprit behind your script not working.

Hmmm…

I’ve tested this further and it appears your right, WaitForChild() does work. In the tests I did at the time of commenting, it didn’t, which is strange. Doing this again the exact opposite occured, the . operator didn’t work, as opposed to :WaitForChild which did.

With some added yield, both worked fine:

task.wait(10)
print(game.Players.LocalPlayer.PlayerGui:GetChildren()) --> {...}
task.wait(10)
print(game.Players.LocalPlayer:WaitForChild("PlayerGui"):GetChildren()) --> {...}

That was a research issue on my end. But now that’s out of the way, I’m quite stuck on figuring out what the issue may be.

pretty sure its becouse of

Anyway, please provide console errors.

Maybe the character already exists and your event isnt firing.

1 Like

Guys i solved it, The issue was that waiting for playergui made it so the characteradded event would be listened later which caused it not to be fired

Thanks for the help everyone

Just realised someone already suggested that lol