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)
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.
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.
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.
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
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