What would be the best way to detect if the player is near a specific type of part, like ones in a folder for example, i have thought about running the script for each part independently, and making specific zones near the parts, but i dont know whats the easiest and best way to do it?
This is the script i’ve been using so far, because i only have one part, it changes the transparency of the selectionbox inside:
local player = game.Players.LocalPlayer
local part = workspace.Doors.d1
local SB = part.SelectionBox
local triggerDistance = 10
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local transparencyTweenInfo = TweenInfo.new(
0.2,
Enum.EasingStyle.Linear,
Enum.EasingDirection.InOut
)
local transparencyTween = tweenService:Create(
SB,
transparencyTweenInfo,
{ Transparency = 0 }
)
local reverseTween = tweenService:Create(
SB,
transparencyTweenInfo,
{ Transparency = 1 }
)
local isTweenPlaying = false
local function calculateDistance()
local partPosition = part.Position
local playerPosition = player.Character and player.Character:FindFirstChild("HumanoidRootPart") and player.Character.HumanoidRootPart.Position
if partPosition and playerPosition then
return (partPosition - playerPosition).Magnitude
else
return math.huge
end
end
local function updateTransparency()
local distance = calculateDistance()
if distance <= triggerDistance and part.Transparency == 0 then
if isTweenPlaying == false then
reverseTween:Cancel()
transparencyTween:Play()
isTweenPlaying = true
wait(0.2)
isTweenPlaying = false
end
elseif distance > triggerDistance then
if isTweenPlaying == false then
transparencyTween:Cancel() -- BUGFIX #1
reverseTween:Play()
isTweenPlaying = true
wait(0.2)
isTweenPlaying = false
end
end
end
local function onRenderStep()
updateTransparency()
end
runService.RenderStepped:Connect(onRenderStep)
EDIT: I used collectionservice since its easier to manage, worked first try like a charm.