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?
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
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
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.
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.
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:
I tried this before you said this, Unfortunately, Now its Infinite Yield Infinite yield possible on 'Workspace.yHaruRBLX.UpperTorso:WaitForChild("NumberBack")' - Studio
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