# How to make a part visible locally only if you are close to it?

Iam trying to create a barrier that can be only visible if you are close to it and if you go far from it goes invisible but I don’t know how I can make that.

You can use magnitude as it returns the length of 2 vectors. Subtract the player’s `HumanoidRootPart` position and the part. Then use magnitude to get the distance and then check if the player is near it or not.

https://developer.roblox.com/en-us/articles/Magnitude

2 Likes

While Silents’ solution would work, if you have a large part, you would need a really large radius in order for the barrier to not turn invisible if you’re near a corner. Doing this would also make the barrier invisible if you’re decently far away, and roughly in line with the center.
A solution to this is to test the distance between the player’s HumanoidRootPart, and the point closest to the player while still inside the barrier.
Implementing this is rather easy, all you need to do is change the player’s position to object space in comparison to the barrier part, and then clamping the player’s position to the size of the cube, and then changing the moved position to be in world space again in comparison to the barrier.

``````function GetClosestPoint(PlayerPos, Barrier) -- Barrier is the barrier object
local RelPoint = Barrier.CFrame:PointToObjectSpace(PlayerPos)
local ClampedPos = Vector3.new(
math.clamp(RelPoint.X, -Barrier.Size.X/2, Barrier.Size.X/2),
math.clamp(RelPoint.Y, -Barrier.Size.Y/2, Barrier.Size.Y/2),
math.clamp(RelPoint.Z, -Barrier.Size.Z/2, Barrier.Size.Z/2)
)
local ClosestPoint = Barrier.CFrame:PointToWorldSpace(ClampedPos)
return ClosestPoint
end
``````

Simply adding this function to your code, and replacing the barrier’s position with the outcome of this function when calculating distance should improve large barriers by a lot.