How would I have a BillboardGui appear over NPC's head?

What do you want to achieve?
I’d like a BillboardGui to appear over an NPC’s (not the players) head when they’re touching an object & then disappear when they’re not touching the object anymore.

What solutions have you tried so far?
I’ve read a few topics on the DevForum about BillboardGui but I haven’t been able to find a topic that was specific to what I’m trying to achieve.

I’ve inserted IntValues named “Num” into each NPC to determine which BillboardGui appears over their head.

Here’s my Explorer setup & the script within the part:

I also apologize if my code is terribly terribly wrong… I’ve just started to try to teach myself how to script and it’s been long/challenging in the beginning.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local NPC = game.Workspace.NPC(hit.Parent)
		if NPC then
			
			local Num = NPC.Num.Value

			if Num == 1 then
				NPC.Head["BillboardGui1"].Enabled = true
			elseif Num == 2 then
				NPC.Head["BillboardGui2"].Enabled = true
			elseif Num == 3 then
				NPC.Head["BillboardGui3"].Enabled = true
			elseif Num == 4  then
				NPC.Head["BillboardGui4"].Enabled = true
			end
			
				script.Parent.TouchEnded:Connect(function(hit2)
					if hit == hit2 then
						NPC.Head["BillboardGui1"].Enabled = false
		
						NPC.Head["BillboardGui2"].Enabled = false
				
						NPC.Head["BillboardGui3"].Enabled = false
				
						NPC.Head["BillboardGui4"].Enabled = false
					end
					end)
			end
		end
end)

Screen Shot 2022-07-15 at 2.54.37 PM

You could set the studs offset to something like Vector3.new(0, 1, 0)

1 Like

Does all of your code work as needed? Does it appear and disappear when needed? If So, You Should Change The Default StudOffset as said by baum.

2 Likes

My script is not working as needed—I forgot to mention that so thank you for asking!

I’ve placed the NPC so that it would be touching the part but the GUI won’t appear. I’m not sure what the specific issue is… perhaps it’s a lot of things I’m not doing right.

I’ve also tried putting the GUI into the NPC’s head; however, it also won’t appear when touching the part either.

Also, how would I change the Default StudOffset in the script? I’m not sure where to put it within my script… again, I’m a newbie :sweat_smile:

If you want you could do it in a script but it’s not needed

2 Likes

Alright, I’ll see if I might be able to help you with it.

2 Likes

I see what you mean! In order to position the GUI correctly. However, my primary issue is that my script isn’t working the way it’s supposed to (I do appreciate the pointers though!)

script.Parent.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
if Hit.Parent.Name == "NPC" then
local NPC = Hit.Parent
local Int = NPC.Num.Value
NPC.Head:FindFirstChild("BillboardGui"..Int).Enabled = true
NPC.Head:FindFirstChild("BillboardGui"..Int).StudsOffset = Vector3.new(0, 1, 0)
script.Parent.TouchEnded:Connect(function(Hit2)
if Hit == Hit2 then
for _, GUIs in pairs(NPC.Head:GetChildren()) do
if GUIs:IsA("BillboardGui") then
GUIs.Enabled = false
end
end
end
end)
end
end
end)
1 Like

It’s better to use something like delay(delay, function) instead of

script.Parent.TouchEnded:Connect(function(hit2)
					if hit == hit2 then
						NPC.Head["BillboardGui1"].Enabled = false
		
						NPC.Head["BillboardGui2"].Enabled = false
				
						NPC.Head["BillboardGui3"].Enabled = false
				
						NPC.Head["BillboardGui4"].Enabled = false
					end
				end)
1 Like

this might fix your script, though I don’t know for certain.

1 Like

But idk what the billboard gui is so maybe @Spectrum99999 way is better

1 Like

Let me try it, thank you so much! Also, it’s telling me I need to define NPC & Num.

Ok, before the ‘Int’, add local NPC = Hit, While for the Num issue, Change ‘Num’ to ‘Int’

1 Like

Still not working :confused:

I’ll include a video of what’s going on!


Screen Shot 2022-07-15 at 3.38.04 PM
This is where I put the script.
Screen Shot 2022-07-15 at 3.37.47 PM
I currently have Num within the NPC model… should I put Num into the Head of the model instead?
Screen Shot 2022-07-15 at 3.37.36 PM
I also put the BillboardGUI into the NPC’s head.

Here’s the script:

script.Parent.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
if Hit.Parent.Name == "NPC" then
local NPC = Hit
local Int = NPC.Num.Value
NPC.Head:FindFirstChild("BillboardGui"..Int).Enabled = true
NPC.Head:FindFirstChild("BillboardGui"..Int).StudsOffset = Vector3.new(0, 1, 0)
script.Parent.TouchEnded:Connect(function(Hit2)
if Hit == Hit2 then
for _, GUIs in pairs(NPC.Head:GetChildren()) do
if GUIs:IsA("BillboardGui") then
GUIs.Enabled = false
end
end
end
end)
end
end
end)

I initially included Num in order to have a specific GUI appear but if the GUI is parented to the NPC’s head then I don’t think I need to include the Num IntValue?

Any errors that might be related in output at all?

Edit: NPC Value should be Hit.Parent.

1 Like

Also, thank you for pointing that out, I’ve fixed that now!

Did the value fix these errors at all?

1 Like

After I fixed the NPC = Hit.Parent, that was the error that was shown.

It’s specifically indicating this line of code:

NPC.Head:FindFirstChild("BillboardGui"..Int).Enabled = true

Currently, I do not have the BillboardGui enabled (since I don’t want it to always be above their head)—perhaps that’s causing an issue?

Before fixing, the error was regarding the Num value but that isn’t showing up in the output anymore so that’s fixed.

Yea it is the Billboard thing. It’s searching for a child of the NPC’s Head named BillboardGui and the IntValue after it.

1 Like

I enabled the GUI; however, now it’s constantly appearing over their head.

Do I need to include another script within the GUI or the part to set it to false before setting it to true?