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

Magnitude on brick is not working and no output, why its not working?

Im trying to check magnitude between brick and player but nothing is happening can someone help me? code :

local plr = game.Players.LocalPlayer

local char = plr.Character or plr.CharacterAdded:wait()

local ReplicatedS = game:GetService("ReplicatedStorage")

local radius = (char.Torso.Position - script.Parent.Position).magnitude

local RS = game:GetService("RunService")

  

RS.RenderStepped:connect(function()

if radius <= 5 then

local bg = Instance.new("BillboardGui")

local text = Instance.new("TextBox",bg)

bg.Parent = script.Parent

text.Text = "Test"

bg.ExtentsOffset = Vector3.new(0,15,0)

end

end)

2 comments

0

Are you sure your character is R6 while testing? Torso is only available when the body rig is R6. Also, you should define radius IN the loop as currently your radius value is constant and is NOT updated on .RenderStepped 

0

tried it with normal loop but its not working

1 answer

✓ Accepted answer

General Practice

Rather than use Torso, you can use the HumanoidRootPart of a Character since these exist in all Humanoid models, while Torso is only found in R6 models

connect is deprecated in favor of Connect

You should not use the parent parameter of Instance.new(), instead define the parent on a separate line

Creating a new Billboard per each Rendering will cause you too spam billboard guis, when you can check it there is one present using :FindFirstChild()

Use :WaitForChild() to make sure a part exists before using / changing it

Consider placing the LocalScript into the StarterGui (the below example specifies the part location rather than using it as a parent)

Issues

You are defining the magnitude of prior to the RenderStepped running, meaning it cannot change after its initial definition. To fix this, place the line of code within the function.

Revised Local Script under StarterGui

local plr = game:GetService("Players").LocalPlayer
local part = workspace:WaitForChild("Part")
local ReplicatedS = game:GetService("ReplicatedStorage")
local RS = game:GetService("RunService")

RS.RenderStepped:Connect(function()
    local root = plr.Character:WaitForChild("HumanoidRootPart")
    local radius = (root.Position - part.Position).magnitude
    if radius <= 5 and part:FindFirstChild("BillboardGui") == nil then
	    local bg = Instance.new("BillboardGui")
	    bg.ExtentsOffset = Vector3.new(0, 15 ,0)
	    bg.Parent = part
	    local text = Instance.new("TextBox")
	    text.Text = "Test"
	    text.Parent = bg
    end
end)

2 comments

0

Its not working

0

Make sure to define the local variable "part" correctly and place the localscript into "StarterGui"

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