Deleting Billboard GUIs in a Player's head

So I wrote this script a few days ago, and it works, apart from that it doesn’t actually delete the gui from the player’s head? Also how is everyone this fine evening :slight_smile:

wait()
game.Workspace.OverheadGUIs.RemoteEvent.OnServerEvent:connect(function(player, cmd)

local ServerStorage = game:GetService("ServerStorage")

local DispatchRoleGUI = ServerStorage.OverheadRolesGUI.DispatchRole
local GuardRoleGUI = ServerStorage.OverheadRolesGUI.GuardRole
local DriverRoleGUI = ServerStorage.OverheadRolesGUI.DriverRole

local character = player.Character

local DispatchRole = false
local DriverRole = false
local GuardRole = false
local NoRole = true

if cmd == "DispatcherRole" then
	
if DriverRole == true then
character.Head:FindFirstChild("DriverRole"):Destroy()
DriverRole = false

elseif GuardRole == true then	
character.Head:FindFirstChild("GuardRole"):Destroy()
GuardRole = false

else
DispatchRole = true
GuardRole = false
DriverRole = false
NoRole = false

local clonedgui = DispatchRoleGUI:Clone()
clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head
	
end	
end

if cmd == "GuardRole" then
	
if DriverRole == true then
character.Head:FindFirstChild("DriverRole"):Destroy()
DriverRole = false

elseif DispatchRole == true then	
character.Head:FindFirstChild("DispatchRole"):Destroy()
DispatchRole = false

else
GuardRole = true
DispatchRole = false
DriverRole = false
NoRole = false
local clonedgui = GuardRoleGUI:Clone()
clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head
end	
end

if cmd == "DriverRole" then
	
if DispatchRole == true then
character.Head:FindFirstChild("DispatchRole"):Destroy()
DispatchRole = false

elseif GuardRole == true then	
character.Head:FindFirstChild("GuardRole"):Destroy()
GuardRole = false

else
DriverRole = true
GuardRole = false
DispatchRole = false
NoRole = false
local clonedgui = DriverRoleGUI:Clone()
clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head	
end	
end

if cmd == "DeleteRole" then
	
if DriverRole == true then
character.Head:FindFirstChild("DriverRole"):Destroy()
DriverRole = false

elseif GuardRole == true then	
character.Head:FindFirstChild("GuardRole"):Destroy()
GuardRole = false

elseif DispatchRole == true then
character.Head:FindFirstChild("DispatchRole"):Destroy()
DispatchRole = false
end	
NoRole = true
end
end)
1 Like

One question, does the script throw any errors?

You could instead use player.Character.Head instead of game.Workspace:WaitForChild(player.Name).Head.

Before I look at the actual issue, there’s a few things that are worrying me a bit.

  • There’s a wait at the top of your script unnecessarily
  • Your RemoteEvents are contained within the Workspace
  • Those remotes have no security whatsoever
  • There’s a lot of nested if statements here and reusable code that can be formed into a function over repeating the same code several times
    • In this regard, your code is also very hard to read
  • Putting a BillboardGui in a player’s head and not even setting the adornee
    • Should be directly under the character with the adornee as the humanoid
  • You’re reinventing the wheel in several circumstances by not making use of certain variables, such as the client that’s passed as the first argument of a remote

That aside, are you able to either pinpoint the location at which your code is supposedly not working or perhaps consider a rewrite of your code to a maintainable format? I’m having a hard time reading it so I can’t quite determine where the issue is at the moment.

Happy Birthday. Also I did try that and that didn’t do anything. There are no errors detected either.

Hi. Sorry for the necropost but did you end up figuring this out? I can’t find any solutions.