Overhead UI Appears on Part Touch: Not Working

I’m trying to make a script that makes a billboardUI appear above a players head once they touch a part, I’m trying to learn scripting, but instead of making the overhead appear above the players head once the part is touched, the script just gives me the overhead as soon as I join.

Example of overhead appearing as soon as I join:

What I’m trying to do is make it appear when the player steps onto the part, I’m trying to figure out what’s going wrong and I’ve tried for about an hour now but just can’t put my finger on it. The script I’m using is below (it’s inside of the part I want the player to touch for the overhead to appear).

local overhead = game.ServerStorage.RedOverhead

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		
		if script.Parent.Touched:Connect() then
			overhead:Clone()
			overhead.Parent = Character.Head
		end
	end)
end)

Any advice or help would be appreciated, thanks! :grin:

4 Likes

Why must you use the Players service? Just connect the .Touched directly.

1 Like

Any advice on how I would do that? Just a bit confused.

Discard the playeradded event as it isnt doing anything

local OverHead = game.ServerStorage.OverHead
local TouchPart = script.Parent

local function Touched(TouchingObject)
	local Character = TouchingObject.Parent
	
-- makes sure whats touching it is a player and makes sure they dont already have an overhead
	if not Character:FindFirstChild("Humanoid") or Character.Head:FindFirstChild("OverHead") then return end
	
	local OverheadClone = OverHead:Clone()
	OverheadClone.Parent = Character.Head
end

-- better practice to declare a function beforehand, cleaner code
TouchPart.Touched:Connect(Touched)
2 Likes

I understand it alot more now, thanks! This worked.

2 Likes

No worries! Added a few comments to further explain.

Hey, I was just checking out the script in-game and it looks like the overhead keeps cloning itself multiple times whilst the player is still on the part, if there any way to prevent this - if so, mind explaining or letting me know? Thanks!

2 Likes

I didn’t adhere to your original instance names, perhaps changing this

Character.Head:FindFirstChild("OverHead")

to this

Character.Head:FindFirstChild(OverHead.Name)

would fix the issue. :+1:

2 Likes

That fixed the issue, I was going to try and make it so the overhead disappears when I step off the part but that isn’t going well either, lol. I changed the script a bit at the end to make the overhead disappear once the player steps off the part, doesn’t seem to work either. Sorry for asking for so much help, but do you think you could try help out/explain how one would do that too, thanks. :+1: (script below)

local OverHead = game.ServerStorage.RedOverhead
local TouchPart = script.Parent
local TouchEnded = script.Parent.TouchEnded

local function Touched(TouchingObject)
	local Character = TouchingObject.Parent

	if not Character:FindFirstChild("Humanoid") or Character.Head:FindFirstChild(OverHead.Name) then return end

	local OverheadClone = OverHead:Clone()
	OverheadClone.Parent = Character.Head
	
	if TouchEnded then
		Character.Head.OverHead:Destroy()
	end
end

TouchPart.Touched:Connect(Touched)

Here!

local Debris = game:GetService("Debris")

local OverHead = game.ServerStorage.RedOverhead
local TouchPart = script.Parent

local function Touched(TouchingObject)
	local Character = TouchingObject.Parent

	if not Character:FindFirstChild("Humanoid") or Character.Head:FindFirstChild(OverHead.Name) then return end

	local OverheadClone = OverHead:Clone()
	OverheadClone.Parent = Character.Head
end

local function TouchEnded(TouchingObject)
	local Character = TouchingObject.Parent
	
	-- making sure its valid
	if not Character:FindFirstChild("Humanoid") then return end
	
	-- Attempts to find the overhead
	local FoundOverHead = Character.Head:FindFirstChild(OverHead.Name)
	if FoundOverHead then
		Debris:AddItem(FoundOverHead, 0)
		-- Debris is a better :Destroy(), doesnt error if the instance no longer exists when destroying it
		-- the second number is the time until it destroys upon declaring, for our purposes we want it to be instant
	end
end

-- Touchended works the same as .touched, needs to be connected.
TouchPart.TouchEnded:Connect(TouchEnded)
TouchPart.Touched:Connect(Touched)

Thanks so much, you helped me developed a much better understanding of everything. I appreciate all your help!

Was trying the script out and it looks like the overheads keeps flashing on and off when I’m on the part, I checked in the explorer and it just keeps dissapearing and re-appearing so maybe it’s destroying itself then instantly re-appearing?

1 Like

That would mean touchended is triggering somehow. perhaps the part isnt anchored?

itd likely have better detection if cancollide was set to false, standing on top of it can cause it to bug out sometimes

Just anchored it and turned cancollide to false, it doesn’t flash anymore when standing still but when walking on the part it starts to flash again

I’ll create another post asking about it, thanks for your help though!

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