How do i make a part appear if i get close and disappear if i walk away?

Hey there! I’m currently trying to make a game and i’m wondering how i could make a model appear if you got close enough to it, and disappear again if you got further away from it. I’m not that good at scripting i mostly build so this is a hard problem to solve. (Yes i’ve tried searching i haven’t found anything)

This is what i’m trying to make:


Appreciate the help, Thanks

6 Likes

Magnitude of part of the character (torso, head, humanoidrootpart, etc.) and the part you want to appear and dissapear.

4 Likes

I barely know any scripting this tell me nothing :confused:

1 Like
while wait() do
local magnitude = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - workspace.PartToChange.Position).Magnitude
if magnitude > 10 then workspace.PartToChange.Transparency = 1 
else workspace.PartToChange.Transparency = 0 
end
end

Put in a local script in StarterPlayerScripts or StarterGui, change 10 to the distance and PartToChange to the part name.

7 Likes

It is bad practice to use while wait() do loops, you should be using either RenderStepped or Heartbeart, depending on your situation from RunService.

So your code would look like this:

local RS = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	local Dist= (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - workspace.PartToChange.Position).Magnitude
	if Dist > 10 then 
		workspace.PartToChange.Transparency = 1
	else
		workspace.PartToChange.Transparency = 0 
	end
end)

Another way you can do this without magnitude is to use DistanceFromCharacter however if the player does not have a character (if they have died for example) it will return 0, tricking your code into thinking they are standing on the object.

4 Likes

This only works with parts, I’m trying to do it with a model

2 Likes

Using the better loop from @Canopius because they are correct about looping:

local RS = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	local Dist= (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - workspace.Model.PartToChange.Position).Magnitude
	if Dist > 10 then 
		for i,v in pairs (workspace.Model:GetChildren()) do 
if v:IsA("BasePart")
 then v.Transparency = 1 
end
end
	else
	for i,v in pairs (workspace.Model:GetChildren()) do 
if v:IsA("BasePart")
 then v.Transparency = 0
end
end
	end
end)

If it’s formatted slightly wrong, I’m sorry, I’m typing this on a phone.

3 Likes

If you are trying to do it with a model all you need to do is loop through all of the base parts in a model and change their transparency:

local Children = workspace.Model:GetChildren() -- Can also use GetDescendants()

for index = 1, #Children() do
	if Children[index]:IsA("BasePart") then
		Children[index].Transparency = 0
	end
end
1 Like

How do I make it to work with folders?

2 Likes

Just change the path that it is looking in, so instead of

workspace.Model:GetChildren()

you could do something like:

workspace.Model["Folder 1 Name"].Folder2Name:GetChildren()
4 Likes

how would i make it fade in instead of abruptly popping into existence

Use TweenService, You can find more info on the devwiki

I am trying to make a barrier appear when someone walks near and disappear when u walk away, Its a very big barrier. Any ideas how to do that? I have these 2 blocks of code but idk how to get them to work together.

local RS = game:GetService("RunService")

RS.RenderStepped:Connect(function()
	local Dist= (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - workspace.Model.PartToChange.Position).Magnitude
	if Dist > 10 then 
		for i,v in pairs (workspace.Model:GetChildren()) do 
			if v:IsA("BasePart")
			then v.Transparency = 1 
			end
		end
	else
		for i,v in pairs (workspace.Model:GetChildren()) do 
			if v:IsA("BasePart")
			then v.Transparency = 0
			end
		end
	end
end)


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

I also need to add a tween service so the closer u get the more visible the part is

Could we use tweenservice to make the part transparency go up smoother?