Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

BillboardGui able to view at certain range?

Asked by
iFlusters 355 Moderation Voter
9 years ago

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:

01local Gui = Instance.new("BillboardGui")
02Gui.Parent = newPlayer.Character.Head
03Gui.Adornee = newPlayer.Character.Head
04Gui.Size = UDim2.new(1, 0, 1, 0)
05Gui.StudsOffset = Vector3.new(0, 2, 0)
06local Text = Instance.new("TextLabel")
07Text.Size = UDim2.new(1, 0, 1, 0)
08Text.BackgroundTransparency = 1
09Text.TextColor3 = Color3.new(230/255, 0/255, 0/255)
10Text.TextStrokeTransparency = 0
11Text.TextTransparency = 0
12Text.FontSize = "Size18"
13Text.Font = "ArialBold"
14local Rank = "[Tag Name]"
15local Person = newPlayer.Name
16 
17Text.Text = (Rank)
18Text.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

Log in to vote
2
Answered by 9 years ago

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

01function createNewGui(newPlayer)
02    local Gui = Instance.new("BillboardGui")
03    Gui.Parent = newPlayer.Character.Head
04    Gui.Adornee = newPlayer.Character.Head
05    Gui.Size = UDim2.new(1, 0, 1, 0)
06    Gui.StudsOffset = Vector3.new(0, 2, 0)
07 
08    local Text = Instance.new("TextLabel")
09    Text.Size = UDim2.new(1, 0, 1, 0)
10    Text.BackgroundTransparency = 1
11    Text.TextColor3 = Color3.new(230/255, 0/255, 0/255)
12    Text.TextStrokeTransparency = 0
13    Text.TextTransparency = 0
14    Text.FontSize = "Size18"
15    Text.Font = "ArialBold"
View all 21 lines...

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

01local maxDistance = 60 -- you can edit this later
02 
03function checkDistance(player1, player2)
04    local character1 = player1.Character
05    local character2 = player2.Character
06    if (character1.Torso.Position - character2.Torso.Position).magnitude <= maxDistance then
07        createNewGui(player2) -- give player2 a gui
08    else
09        -- take away player2's gui
10        local trash = player2.Character.Head:FindFirstChild('BillboardGui')
11 
12        if trash then
13            trash.Parent = nil
14        end
15    end
16end

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

01local me = game.Players.LocalPlayer -- gets you as a player
02local renderTime = 20
03 -- 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.
04 
05while wait(1/renderTime) do
06    for i, v in pairs(game.Players:GetPlayers()) do
07        if v.Name ~= me.Name then
08            checkDistance(me, v)
09        end
10    end
11end

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

Final Product

01local me = game.Players.LocalPlayer -- gets you as a player
02local renderTime = 20
03local maxDistance = 60
04 
05function createNewGui(newPlayer)
06    local Gui = Instance.new("BillboardGui")
07    Gui.Parent = newPlayer.Character.Head
08    Gui.Adornee = newPlayer.Character.Head
09    Gui.Size = UDim2.new(1, 0, 1, 0)
10    Gui.StudsOffset = Vector3.new(0, 2, 0)
11 
12    local Text = Instance.new("TextLabel")
13    Text.Size = UDim2.new(1, 0, 1, 0)
14    Text.BackgroundTransparency = 1
15    Text.TextColor3 = Color3.new(230/255, 0/255, 0/255)
View all 48 lines...

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

0
Works great, is there a way to increase the time on checkDistance, when testing I notice a delay. iFlusters 355 — 9y
0
try increasing renderTime to any value between 30 and 60 maybe just play around with it and see what you like ProfessorSev 220 — 9y
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} yoshi8080 445 — 9y
0
I haven't heard of that ill look into later ProfessorSev 220 — 9y
Ad

Answer this question