Problems with Overhead GUI

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local MarketplaceService = game:GetService("MarketplaceService")

local OverheadGui = ServerStorage.BillboardGui

Players.PlayerAdded:Connect(function (Player)
    Players.CharacterAdded:Connect(function (Character)
        local success, result = pcall(MarketplaceService.UserOwnsGamePassAsync, MarketplaceService, Player.UserId, 529886)
        if success and result then
            local Overhead = BillboardGui:Clone()
            OverheadGui.TextLabel.Text = "VIP"
            OverheadGui.TextLabel.TextColor3 = Color3.fromRGB(45, 255, 90)
            OverheadGui.Adornee = Character.Head
            OverheadGui.Parent = Character
        end
    end)
end)

Some pointers Lots of pointers I’d like to give, both to code that was posted before my own and in general. Normally I do not like to write out entire blocks of code for others, but since it was already done, I just refurbished the code.

  • Using a wait statement after CharacterAdded is unnecessary. Cloning is a synchronous operation and CharacterAdded fires as in when the character is set up and assigned.
  • Try indexing services as local variables, should you need them later into your script.
  • The name convention for “game pass” is two words. Game pass, not gamepass.
  • UserOwnsGamePassAsync is a web call internally. You should wrap any kind of internal web calls in pcall so that errors can be caught if the call fails.
  • Use better names. “BillboardGui” is ambiguous for an object that you’re storing directly under ServerStorage. Always use clear and concise naming for variables. It helps you and if you have any, your collaborators as well.
  • If you’re going for a pre-setup object that doesn’t need to be modified later, set the properties. You can skip using code space to edit something that can be already be set at runtime.
  • Set your properties first before the Parent of an object.
  • Proofread your code and do debugging. Often at times, some code errors may be an oversight on your behalf. As well, when someone suggests code, play around with it a bit - don’t copy it raw into Studio, test it and say “it doesn’t work” immediately after (that’s if the code isn’t intended to be utilised this way).
  • Search the DevForum and the Developer Hub. Often, these two locations already hold answers to your problems as asked by others.
  • Check everything. What if your only “error” happened to be that the BillboardGui’s Enabled property was set to false!?
  • Search the Developer Console when running a test server. Your console could be spitting out errors regarding what’s happening with your code.
  • Place prints around your code and see what prints and what doesn’t. Printing helps a lot to set a checkpoint as to where your code is and isn’t working.
  • In the future, in regards to one of your attempts to play with your code, any kind of work that has to do explicitly with getting a Player for a permission-based asset, always do it by UserId. Username can be changed, while UserId cannot. You can convert a username to a UserId by using GetUserIdFromNameAsync. Whether your username changes or not, this feeds a username to the function and spits back out the UserId associated with this username. It’s internally a web call, so remember to pcall it.
5 Likes