Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

BillboardGui able to view at certain range?

Like the basic roblox player name, if you're a certain distance from it, it won't show. I have a BillboardGui above certain player's names displaying a Tag but here's what comes up:

http://prntscr.com/9th8m2 http://prntscr.com/9th8s1

The code below is in a 'Script' in Workspace

Here's the code:

local Gui = Instance.new("BillboardGui")
Gui.Parent = newPlayer.Character.Head
Gui.Adornee = newPlayer.Character.Head
Gui.Size = UDim2.new(1, 0, 1, 0)
Gui.StudsOffset = Vector3.new(0, 2, 0)
local Text = Instance.new("TextLabel")
Text.Size = UDim2.new(1, 0, 1, 0)
Text.BackgroundTransparency = 1
Text.TextColor3 = Color3.new(230/255, 0/255, 0/255)
Text.TextStrokeTransparency = 0
Text.TextTransparency = 0
Text.FontSize = "Size18"
Text.Font = "ArialBold"
local Rank = "[Tag Name]"
local Person = newPlayer.Name

Text.Text = (Rank)
Text.Parent = Gui	

I am only guessing it will involve Magnitude but I am not sure how to implement it, any help would be great.

1 answer

✓ Accepted answer

So your right that it will involve magnitude but it also requires a few things so i am going to walk you through what you need to add to make it work like you want.

The Problem

so we want a billboard gui that is visible locally meaning only the player can see it. To achieve this we need to use local parts which can be created by setting filtering enabled to true then creating the gui from the client.

The Script

so to start lets make filtering enabled = true. this will allow for local parts. now lets create a function that will make the gui so we can use it later

function createNewGui(newPlayer)
	local Gui = Instance.new("BillboardGui")
	Gui.Parent = newPlayer.Character.Head
	Gui.Adornee = newPlayer.Character.Head
	Gui.Size = UDim2.new(1, 0, 1, 0)
	Gui.StudsOffset = Vector3.new(0, 2, 0)

	local Text = Instance.new("TextLabel")
	Text.Size = UDim2.new(1, 0, 1, 0)
	Text.BackgroundTransparency = 1
	Text.TextColor3 = Color3.new(230/255, 0/255, 0/255)
	Text.TextStrokeTransparency = 0
	Text.TextTransparency = 0
	Text.FontSize = "Size18"
	Text.Font = "ArialBold"

	local Rank = "[Tag Name]"
	local Person = newPlayer.Name
	Text.Text = (Rank)
	Text.Parent = Gui   
end

simple enough so now onto your initial problem giving each player a distance limiter. to do this we can create another function called check distance

local maxDistance = 60 -- you can edit this later

function checkDistance(player1, player2)
	local character1 = player1.Character
	local character2 = player2.Character
	if (character1.Torso.Position - character2.Torso.Position).magnitude <= maxDistance then
		createNewGui(player2) -- give player2 a gui
	else
		-- take away player2's gui
		local trash = player2.Character.Head:FindFirstChild('BillboardGui')

		if trash then
			trash.Parent = nil 
		end
	end
end

next i am going to set up a while true do loop. what this loop will do is loop through every player and run it through our checkDistance function which calls our createNewGui function

local me = game.Players.LocalPlayer -- gets you as a player
local renderTime = 20
 -- number of times per second the loop runs, i personally recommend 20 because that is 20 frames per second which is a smooth number. anything extreme is just wasteful.

while wait(1/renderTime) do
	for i, v in pairs(game.Players:GetPlayers()) do
		if v.Name ~= me.Name then
			checkDistance(me, v)
		end
	end
end

and that would be your loop so we can combine everything here to come up with this

Final Product

local me = game.Players.LocalPlayer -- gets you as a player
local renderTime = 20
local maxDistance = 60

function createNewGui(newPlayer)
	local Gui = Instance.new("BillboardGui")
	Gui.Parent = newPlayer.Character.Head
	Gui.Adornee = newPlayer.Character.Head
	Gui.Size = UDim2.new(1, 0, 1, 0)
	Gui.StudsOffset = Vector3.new(0, 2, 0)

	local Text = Instance.new("TextLabel")
	Text.Size = UDim2.new(1, 0, 1, 0)
	Text.BackgroundTransparency = 1
	Text.TextColor3 = Color3.new(230/255, 0/255, 0/255)
	Text.TextStrokeTransparency = 0
	Text.TextTransparency = 0
	Text.FontSize = "Size18"
	Text.Font = "ArialBold"

	local Rank = "[Tag Name]"
	local Person = newPlayer.Name
	Text.Text = (Rank)
	Text.Parent = Gui   
end

function checkDistance(player1, player2)
	local character1 = player1.Character
	local character2 = player2.Character
	if (character1.Torso.Position - character2.Torso.Position).magnitude <= maxDistance then
		createNewGui(player2) -- give player2 a gui
	else
		-- take away player2's gui
		local trash = player2.Character.Head:FindFirstChild('BillboardGui')

		if trash then
			trash.Parent = nil 
		end
	end
end

while wait(1/renderTime) do
	for i, v in pairs(game.Players:GetPlayers()) do
		if v.Name ~= me.Name then
			checkDistance(me, v)
		end
	end
end

hope this helped remember to set filtering enabled to true otherwise it wont work

4 comments

0

Works great, is there a way to increase the time on checkDistance, when testing I notice a delay.

0

try increasing renderTime to any value between 30 and 60 maybe just play around with it and see what you like

0

There is another way to make it viewable without magnitude, I think it was to set the billboardgui or text label to {0,0}{0,-10}

0

I haven't heard of that ill look into later

This discussion is preserved as it was. New answers, comments, and votes are closed.