Hi!
I have custom Nametags on my game (shows Username and Group Rank).
They are Billboard GUIs with a TextLabel inside.
I want to make a ImageButton that when clicked hides and unhides only those Billboards that are inside a Script in the Workspace (because I have more Billboards in the Map that I don’t want to hide).
Can someone help me make a LocalScript for it?
I know how to make the Button, i just need help with the coding (I’m learning while doing some small projects).
Thanks
Image Button in Screen Gui???
Yes, I want to make a ImageButton that is inside a ScreenGui in the StarterGui.
Ok then
-- LocalScript in ImageButton
local ImageButton = script.Parent
local players = game:GetService("Players")
local on = false
ImageButton.MouseButton1Click:Connect(function()
if on then
on = false
for i, v in ipairs(players:GetChildren()) do
local character = v.Character or p.CharacterAdded:Wait()
character.Head.BillboardGui.Enabled = false
end
else
on = true
for i, v in ipairs(players:GetChildren()) do
local character = v.Character or p.CharacterAdded:Wait()
character.Head.BillboardGui.Enabled = true
end
end
end)
Reply if any error
Notes:
- It will work if your tag name is “BillboardGui” and it’s parent is character head
- It don’t hide player tags when they join and it’s turned on
Do I have to add a variable for p?
You don’t need because it’s LocalScript and you can call LocalPlayer
Ok.
I have an error: Players.ZuperManoz.PlayerGui.HideNamesButton.HideButton.GuiChange:17: attempt to index nil with ‘Character’
And on your script I changed character.Head.BillboardGui.Enabled to character.Head.GroupRankOverHead.Enabled because it is the BillboardGUI name.
Sorry, you were right.
I didn’t notice that, I fixed the script. Try it now.
Now it did work!
Thank you!
1 Like
Your code could easily be simplified into this
local Players = game:GetService("Players")
local button = script.Parent
button.Activated:Connect(function()
for _, player in ipairs(Players:GetPlayers()) do
local character = player.Character
local billboard = character and character.Head:FindFirstChild("BillboardGui")
if not billboard then
continue
end
billboard.Enabled = not billboard.Enabled
end
end)
And for @ZuperManoz if you want to include functionality to keep the boards disabled when palyers join and/or they respawn, you’ll need to create events that listen for when their character is added and when a player joins that cause their billboard to be toggled.
Remember to change BillboardGui
to the name of your BillboardGui that is inserted in the character’s head
3 Likes
Thank you too!
I will test the code.
What that means and
in this fragment?
In this case it means,
billboard
is assigned to the result of FindFirstChild("BillboardGui")
IF character
is not nil
or false
, otherwise assign billboard
to whatever character
is
Let’s do another example
local var = true
local var2 = var and 15
print(var2)
If you try this, it will print out 15
because var
was truthy and continued to 15
, but let’s say you change var
to false
or nil
local var = false
local var2 = var and 15
print(var2)
Now this will print out whatever you stored in var
, if you stored false
, it would print false
, if you stored nil
, it will print out nil
1 Like
Thank you, I will definitely use it in the future
2 Likes
I created for you event that will hide the billboard gui when player joins and button is turned on:
game:GetService("Players").PlayerAdded:Connect(function(p)
if on then
local character = p.Character or p.CharacterAdded:Wait()
character.Head.BillboardGui.Enabled = false
end
end)
Put it after previous script, report any errors.
Btw: This GUI will reset when player die
Thanks, I’m not sure if I need this, but I will test it!