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
function checkDistance(player1, player2)
local character1 = player1.Character
local character2 = player2.Character
if (character1.Torso.Position - character2.Torso.Position).magnitude <= maxDistance then
createNewGui(player2)
else
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
local renderTime = 20
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
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)
else
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
Works great, is there a way to increase the time on checkDistance, when testing I notice a delay.
try increasing renderTime to any value between 30 and 60 maybe just play around with it and see what you like
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}
I haven't heard of that ill look into later