Script can't find ScreenGui

I have a script that is meant to make a GUI visible to a player when an OnClientEvent occurs. However, the script keeps erooring stating that "ScreenGui is not a valid member of (playername).PlayerGui, despite the fact that it clearly is. I’m at a loss here so any help would be greatly appreciated!

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local challengeRecieved = game.ReplicatedStorage.PlayerChallengedClientRecieve
local challengeAnnounce = LocalPlayer.PlayerGui.ScreenGui.ChallengeAnnouncePlayer

challengeRecieved.OnClientEvent:Connect(function(Player, name)
	local challengeUI = LocalPlayer.PlayerGui.ScreenGui.ChallengeAnnouncePlayer
	
	challengeUI.Visible = true
	
	challengeUI.YouveBeenChallenged.Text = ("You have been challenged by" ..Player)
end)

challengeAnnounce.ChallengeAccept.MouseButton1Down:Connect(function()
	print("Player has accepted the challenge!")
	
	local battleQueue = game.ReplicatedStorage.BattleQueue
	
	
	
	battleQueue:FireServer()
	
end)

challengeAnnounce.ChallengeDeny.MouseButton1Down:Connect(function()
	print("Player has denied the challenge!")
	
	challengeAnnounce.Visible = false
end)
1 Like

Can you show a screenshot of the instance existing while the error happens?

1 Like

Blockquote

local Players = game:GetService(“Players”)
local LocalPlayer = Players.LocalPlayer

local challengeRecieved = game.ReplicatedStorage:WaitForChild(“PlayerChallengedClientRecieve”)
local challengeAnnounce = LocalPlayer:WaitForChild(“PlayerGui”):WaitForChild(“ScreenGui”):WaitForChild(“ChallengeAnnouncePlayer”)

challengeRecieved.OnClientEvent:Connect(function(Player, name)
challengeAnnounce.Visible = true
challengeAnnounce.YouveBeenChallenged.Text = "You have been challenged by " … Player.Name
end)

challengeAnnounce.ChallengeAccept.MouseButton1Down:Connect(function()
print(“Player has accepted the challenge!”)
local battleQueue = game.ReplicatedStorage:WaitForChild(“BattleQueue”)
battleQueue:FireServer()
end)

challengeAnnounce.ChallengeDeny.MouseButton1Down:Connect(function()
print(“Player has denied the challenge!”)
challengeAnnounce.Visible = false
end)
‘’’

1 Like

Don’t mind the weird button placement scaling has been an issue for me lol

1 Like

If I’m not mistaken, the reason your script says it’s not there is because your script is executing code before the UI has loaded into your character.

To fix this, use :WaitForChild(UI) (this will yield the script’s execution on the current thread until it appears). Here’s the documentation for it.

The code provided by @Slymbiote should also do the trick.

Hope this helps!

3 Likes
local Players = game:GetService(“Players”)
local LocalPlayer = Players.LocalPlayer

local challengeRecieved = game.ReplicatedStorage:WaitForChild(“PlayerChallengedClientRecieve”)
local challengeAnnounce = LocalPlayer:WaitForChild(“PlayerGui”):WaitForChild(“ScreenGui”):WaitForChild(“ChallengeAnnouncePlayer”)

challengeRecieved.OnClientEvent:Connect(function(Player, name)
challengeAnnounce.Visible = true
challengeAnnounce.YouveBeenChallenged.Text = "You have been challenged by " … Player.Name
end)

challengeAnnounce.ChallengeAccept.MouseButton1Down:Connect(function()
print(“Player has accepted the challenge!”)
local battleQueue = game.ReplicatedStorage:WaitForChild(“BattleQueue”)
battleQueue:FireServer()
end)

challengeAnnounce.ChallengeDeny.MouseButton1Down:Connect(function()
print(“Player has denied the challenge!”)
challengeAnnounce.Visible = false
end

retyped from my pc. Try this bro. :slight_smile:

2 Likes
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rep=game:GetService("ReplicatedStorage")


local challengeRecieved = rep:WaitForChild("PlayerChallengedClientRecieve")
local challengeAnnounce = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
	:WaitForChild("ChallengeAnnouncePlayer")
..
..

Now we know everything is set up and can move on …
Also this: local challengeUI = LocalPlayer.PlayerGui.ScreenGui.ChallengeAnnouncePlayer
has already been defined as challengeAnnounce

You did this so fast it didn’t get the gui location, so when you referenced it later, it had no clue.

Also I’m stalling with humanoid because it’s a great stall.

The reason this stuff is needed is it takes a little bit for the player to completely load into the game. Your error is a dead giveaway you did not give that the time it needed.

3 Likes

Thanks bro! However, OP ask for help, we give OP the help he asked for. OP does not respond to our solutions. :face_with_raised_eyebrow:

As long as he understand what happen here I’m ok with that. I think we all make this mistake at 1st.

1 Like

Sorry, stuff happened last night. Didn’t get time. However, the WaitForChild() statement worked, it seems that the script was being executed faster than it could all be loaded in. Not sure why I didn’t think of this as it has happened to me before, but I am grateful for the help you guys gave me here. The code is working again and all is good. Apologies for not responding last night, as I said stuff was going on for me all of a sudden and I just couldn’t find the time. Thank you all.

No worries, I was wondering if the fix worked. I’m glad it worked for you. Please mark the one that helped as the solution for others to see.

:sunglasses:

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