SurfaceGUI Problem

Hello, I require a help with Surface GUIs
I made a Surface GUI with a text label on the player’s back (Actually parented in player’s UpperTorso)
However, When in First Person and the player looks down and runs, The number is visible on the first person, Is there any way to fix this with scripting?

Any help is appreciated.

You can just disable it yourself no?

This is the worst idea by far…

That seems tedious, I don’t think this idea is good.

I don’t really know how would i do this.

I know right.

Just toggle the enabled property based on how close the camera is to the head. There are tutorials and scripts online.

Hey, yHaruRBLX!

A great way to achieve this is to use a LocalScript and then change the Enabled property of the SurfaceGui. To detect if the player is in first-person, you can simply get the distance between the player’s camera and their head and see if it’s less than 1 stud.

Something like:

-- Variables.
local Game,Workspace.Script = game,workspace,script
local RunService = Game:GetService("RunService")
local Camera = Workspace.CurrentCamera

local Character = Script.Parent
local Head = Character:WaitForChild("Head")
local UpperTorso = Character:WaitForChild("UpperTorso")
local Gui = UpperTorso["<YourGuiName>"]

local function ToggleState()
	if (Camera.CFrame.Position - Head.Position).Magnitude < 1 then
		Gui.Enabled = false
	else
		Gui.Enabled = true
	end
end

RunService.RenderStepped:Connect(ToggleState)

Make sure this belongs in StarterPlayer > StarterCharacterScripts

1 Like

Thank you, Cloudy. I appreciate the help. I will try your idea.

No worries, just remember to make the script a LocalScript inside of StarterPlayer > StarterCharacterScripts.

Hey, I tested your idea and… It doesn’t work.
When i checked the Console, I found this error.
13:56:07.225 Workspace.yHaruRBLX.LocalScript:1: Expected identifier when parsing expression, got '.' - Studio - LocalScript:1

I put the name of the SurfaceGUI which is NumberBack

Replace the first line with:

local Game,Workspace,Script = game,workspace,script

Just a tip but try to fix such errors yourself as it would help you understand script better.
An easy way is to just try to understand the error. The error states that: “There was a variable expected at line 1 but got “.” (period)”
We understand from this that there happened to be period or full-stop where there was supposed to be a comma to separate the variable. The fix, is to just replace the comma.

Alright, you can change it yourself.

The error is gone.
Though now it says: 14:07:17.925 NumberBack is not a valid member of MeshPart "Workspace.yHaruRBLX.UpperTorso" - Client - LocalScript:8
Even though, its clearly inside the player’s UpperTorso.

Of course! How silly of me to not consider this.

Basically, the error occurs because the Gui doesn’t load in time for the script to execute and thus causes this error. An easy way around this is to encapsulate “NumberBack” within a :WaitForChild method. Just like this:

local Gui = UpperTorso:WaitForChild("NumberBack")

I tried this before you said this, Unfortunately, Now its Infinite Yield
Infinite yield possible on 'Workspace.yHaruRBLX.UpperTorso:WaitForChild("NumberBack")' - Studio

Hmm… Interesting. Can you check again if NumberBack really exists? This error only occurs if NumberBack doesn’t exist at all!

Perhaps a spelling mistake or sort?

The names match, I don’t know what causes the issue.

after this line add print(Gui)
if the code can go through and print out its name, then it is just a harmless warning, and it means the character and stuffs are loading not fast enough

in this case, you might use the following to suppress the warning

while not UpperTorso:FindFirstChild("NumberBack") do task.wait() end
local Gui = UpperTorso.NumberBack

After what line? Im a bit confused.

Such a warning is not “useless”. Printing the variable after the warning won’t print it.

Rather, OP @yHaruRBLX, should do: print(UpperTorso:GetChildren() after the line where we do local UpperTorso = ///.