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.
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)
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)
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!
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. (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)
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)
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?