Trouble with Overhead GUI

Hello dear Scripters,
I got hired to work for a Hangout Game and the first thing to script is an Overhead GUI. But sadly I came accros some trouble with it. On respawning the character you get the touble amount of Icons appear and after respawning any other time the Overhead GUI does not even appear above your head. I will attach the script and a picture of what it looks like up on joining (what it should look like after respawning as well) and the second picture will be showing a picture after respawning for the first time.

The Script

local Boosters = {
	"tr2vl"
}
local Group = 13319744
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OverHeadGUI = ReplicatedStorage:WaitForChild("OverheadGUI")
local Clone = OverHeadGUI:Clone()
local Admin = ReplicatedStorage:WaitForChild("Admin")
local Boost = ReplicatedStorage:WaitForChild("Boost")
local Creator = ReplicatedStorage:WaitForChild("Creator")
local Premium = ReplicatedStorage:WaitForChild("Premium")
local MarketPlaceService = game:GetService("MarketplaceService")

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		
		--Set Name and DisplayName
		Clone.MainFrame.Username.Text = Player.Name.." (@"..Player.DisplayName..")"
		
		--Rank Dsiplay
		if Player:GetRankInGroup(Group) == 255 then
			Clone.MainFrame.Rank.Text = "[H] Group Holder"
		end
		if Player:GetRankInGroup(Group) == 254 then
			Clone.MainFrame.Rank.Text = "[O] Owners"
		end
		if Player:GetRankInGroup(Group) == 253 then
			Clone.MainFrame.Rank.Text = "[C] Community Manager"
		end
		if Player:GetRankInGroup(Group) == 252 then
			Clone.MainFrame.Rank.Text = "[A] Administrator"
		end
		if Player:GetRankInGroup(Group) == 251 then
			Clone.MainFrame.Rank.Text = "Helper"
		end
		if Player:GetRankInGroup(Group) == 2 then
			Clone.MainFrame.Rank.Text = "[F] Friends"
		end
		if Player:GetRankInGroup(Group) == 1 then
			Clone.MainFrame.Rank.Text = "[M] Member"
		end
		
		--Icons
		if Player:GetRankInGroup(Group) >= 251 then
			local AdminClone = Admin:Clone()
			AdminClone.Parent = Clone.MainFrame.Icons
		end
		if Player:GetRankInGroup(Group) >= 253 then
			local CreatorClone = Creator:Clone()
			CreatorClone.Parent = Clone.MainFrame.Icons
		end
		if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, 26586431) then
			local PremiumClone = Premium:Clone()
			PremiumClone.Parent = Clone.MainFrame.Icons
		end
		for _,v in pairs(Boosters) do
			if Player.UserId == game.Players:GetUserIdFromNameAsync(v) then
				local BoostClone = Boost:Clone()
				BoostClone.Parent = Clone.MainFrame.Icons
			end
		end
		
		--Add to Character
		Clone.Parent = Character.Head
	end)
end)
3 Likes

image

If you should need it by the way.

local Clone = OverHeadGUI:Clone()
...
Clone.MainFrame.Username.Text = Player.Name.." (@"..Player.DisplayName..")"
...
Clone.Parent = Character.Head

You’re using the same clone of the overhead gui for each player – perhaps you meant to :Clone a new one per player?

Player.CharacterAdded:Connect(function(Character)
    local NewOverHeadGUI = OverHeadGUI:Clone()
    ...
end)

That’s pretty inefficient and wasteful when you could use a pre-made asset.

EDIT
Taking a look at the program again, this could be programmed more efficiently (taking out the for loop basically)

local Boosters = {
	"tr2vl"
}
...

for _,v in pairs(Boosters) do
	if Player.UserId == game.Players:GetUserIdFromNameAsync(v) then
		local BoostClone = Boost:Clone()
		BoostClone.Parent = Clone.MainFrame.Icons
	end
end

A dictionary could be used in-place, instead of using a for loop and iterating over an array.

local Boosters = {
    [1326865471] = true, -- @tr2vl
}
...
if Boosters[Player.UserId] ~= nil then
	local BoostClone = Boost:Clone()
	BoostClone.Parent = NewOverHeadGUI.MainFrame.Icons
end
2 Likes

You can check if the player already has it and only add it if the player hasn’t got it. I used that tactic for many of my local scripts and server scripts, and it works well.

1 Like

Alright, that helped me but after respawning the UI does not appear at all anymore.|

local Boosters = {
	"tr2vl"
}
local Group = 13319744
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OverHeadGUI = ReplicatedStorage:WaitForChild("OverheadGUI")
local Admin = ReplicatedStorage:WaitForChild("Admin")
local Boost = ReplicatedStorage:WaitForChild("Boost")
local Creator = ReplicatedStorage:WaitForChild("Creator")
local Premium = ReplicatedStorage:WaitForChild("Premium")
local MarketPlaceService = game:GetService("MarketplaceService")

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Clone = OverHeadGUI:Clone()
		
		--Set Name and DisplayName
		Clone.MainFrame.Username.Text = Player.Name.." (@"..Player.DisplayName..")"
		
		--Rank Dsiplay
		if Player:GetRankInGroup(Group) == 255 then
			Clone.MainFrame.Rank.Text = "[H] Group Holder"
		end
		if Player:GetRankInGroup(Group) == 254 then
			Clone.MainFrame.Rank.Text = "[O] Owners"
		end
		if Player:GetRankInGroup(Group) == 253 then
			Clone.MainFrame.Rank.Text = "[C] Community Manager"
		end
		if Player:GetRankInGroup(Group) == 252 then
			Clone.MainFrame.Rank.Text = "[A] Administrator"
		end
		if Player:GetRankInGroup(Group) == 251 then
			Clone.MainFrame.Rank.Text = "Helper"
		end
		if Player:GetRankInGroup(Group) == 2 then
			Clone.MainFrame.Rank.Text = "[F] Friends"
		end
		if Player:GetRankInGroup(Group) == 1 then
			Clone.MainFrame.Rank.Text = "[M] Member"
		end
		
		--Icons
		if Player:GetRankInGroup(Group) >= 251 then
			local AdminClone = Admin:Clone()
			AdminClone.Parent = Clone.MainFrame.Icons
		end
		if Player:GetRankInGroup(Group) >= 253 then
			local CreatorClone = Creator:Clone()
			CreatorClone.Parent = Clone.MainFrame.Icons
		end
		if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, 26586431) then
			local PremiumClone = Premium:Clone()
			PremiumClone.Parent = Clone.MainFrame.Icons
		end
		for _,v in pairs(Boosters) do
			if Player.UserId == game.Players:GetUserIdFromNameAsync(v) then
				local BoostClone = Boost:Clone()
				BoostClone.Parent = Clone.MainFrame.Icons
			end
		end
		
		--Add to Character
		Clone.Parent = Character.Head
	end)
end)

Were there any errors in the output? The only possible error that I can see occurring is the Head not loading in time, which can be rectified with local Head = Character:WaitForChild("Head").

Nope, there were none. But let me try to change it.

Did applying that make any difference? If not, try setting up some prints to see what executes and what doesn’t.
I.e.

print(Player.Name, "has entered the game")
...
print(Player.Name, "'s character (re)spawned")
...
print("Set rank text")
...
print("Finished setting up icons")
...
print("Parented GUI to", Player.Name, "'s head")