How to make distance for highlight

Hello devforum!

I want to use highlight to a part but the problem is that I want it to be seen from a specific number of studs. So if you are like for example, 5 studs away from the part, then you cant see the highlight around it.
I tried searching on devforum for a similar post but I couldn’t find anything.

I would appreciate any help. Thanks!

Nobody has ever tried to this before, that’s why you couldn’t find anything related to this. But logically you should research a bunch of different topics that would help you do this.

These things will help you make this:

game:GetService("RunService").RenderStepped
(camera.CFrame.Position - part.Position).Magnitude
Highlight.Enabled or Highlight.Parent

You would need to use all of these to make the script you want.

I’m kinda new to scripting, could you make an example script so I could understand how all of those work?

Renderstepped will run every frame, which means that you will check on every frame the distance of the highlights and disable it or enable it based on the magnitude of the object.

game:GetService("RunService").RenderStepped -- Event Function for running every frame

(camera.CFrame.Position - part.Position).Magnitude -- Shows the distance between the camera and a part

Highlight.Enabled or Highlight.Parent -- Enables or disables the highlight based on the parent or enable property
1 Like

how will i be able to use magnitude with studs?

If you get the magnitude it gives you the distance in studs already

1 Like

I tried to make a functional script and I ended up with this :

local player = game.Players.LocalPlayer
local MinDistance = 0  
local MaxDistance = 6  

game:GetService("RunService").Heartbeat:Connect(function()
	if player.Character.PrimaryPart == nil then return end
local character = player.Character or player.CharacterAdded:Wait()
local humanoidP = character.PrimaryPart
local part = game.Workspace["E to Open Door"] -- Put your part
local distance = (humanoidP.Position - part:GetPrimaryPartCFrame()).Magnitude -- Get distance
part.Highlight.Enabled = math.clamp(distance, MinDistance, MaxDistance) / MaxDistance
end)

But I get this error : attempt to index nil with ‘Character’

Is this a local script or a server script?

1 Like

This is a server script inside the model I want to hightlight. The description is kinda miss-leading because I changed from part to model. Also the script is obviously from another post with the same subject.

You are not able to get the localplayer with a server script, and if you change it to highlight with a server script everybody will see it so I suggest you use a local script

Edit: Sorry, forgot to mention that the local script can only be in some places, it cannot work unless it is parented to the character, playerscripts folder or the startergui.

1 Like

This past days I’ve been trying to get it to work. And now I have this

local player = game.Players.LocalPlayer
local MinDistance = 0  
local MaxDistance = 6  

game:GetService("RunService").Heartbeat:Connect(function()
	if player.Character.PrimaryPart == nil then return end
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidP = character.PrimaryPart
	local part = game.Workspace.Door
	local distance = (humanoidP.Position - part:GetModelCFrame()).Magnitude
	part.Highlight.Enabled = math.clamp(distance, MinDistance, MaxDistance) / MaxDistance
end)

I keep getting this error that says : invalid argument #1 (CFrame expected, got Vector3)

You’re gettting the cframe instead of the position for the distance, I suggest doing cframe.position

The problem is on this line:

local distance = (humanoidP.Position - part:GetModelCFrame()).Magnitude

I suggest doing this:

local distance = (humanoidP.Position - part:GetModelCFrame().Position).Magnitude

The reason why it doesn’t work is because it expected a position, but you gave the cframe, cframe is orientation and position mixed together, but when using magnitude it expects 2 positions.

1 Like

attempt to index nil with ‘PrimaryPart’ this is the new error i get

1 Like

The primarypart property doesn’t always load in fast enough, I suggest waiting for the humanoidrootpart:

if player.Character:WaitForChild("HumanoidRootPart") == nil then return end

So where am I supposed to put this exactly?

It’s on your if statement on line 6

1 Like

I get an error saying attempt to index nil with ‘WaitForChild’ in the 6th line

I see, the character must also not be loading in fast enough, to fix this, I would wait for the character as well, this can be done by doing:

local Character = player.Character or player.CharacterAdded:Wait()

You should put this line after you declare the player variable and then change your if statement to this:

if Character.PrimaryPart == nil then return end

May I also ask what the if statement is for?

1 Like

Actually not true. You can use a server script and change where it will run. That’s really handy for the problem described here, and I would recommend using it.

1 Like

So run this on a server script, but with a local/client environment. It should honestly just work, with a chance of needing to wait for the player.

1 Like