How will I get around making a script for the transparency to decrease upon getting close to a part?

oh yea let me edit it one last time, I got this

local player = game.Players.LocalPlayer
local MinDistance = 0  -- change the minimum distance
local MaxDistance = 50  -- change the maximum distance

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 = workspace.Part -- Put your part
   local distance = (humanoidP.Position - part.Position).Magnitude -- Get distance
   part.Transparency = math.clamp(distance, MinDistance, MaxDistance) / MaxDistance
end)

all I did was remove the “1 - “

2 Likes

It works like a charm! I have multiple barriers in my game and I do not know if just duplicating the script it going to be particularily efficient…

want me to make the code a bit more efficient?

I have 5 more barriers in my game, and on top of that, since it is a creation game, There will be barriers that WILL appear but I do not know how to get multiple things to do a single command. Probably something with tables…

I’ll make it more efficient then, so you get less lag

local player = game.Players.LocalPlayer
local LastPosition = nil
local debounce = false

local MinDistance = 0  -- change the minimum distance
local MaxDistance = 50  -- change the maximum distance

game:GetService("RunService").Heartbeat:Connect(function()
   local character = player.Character or player.CharacterAdded:Wait()
   if character.PrimaryPart == nil then return end
   local humanoidP = character.PrimaryPart
   if humanoidP.Position ~= LastPosition and debounce == false then
      debounce = true
      LastPosition = humanoidP.Position
      local part = workspace.Part -- Put your part
      local distance = (humanoidP.Position - part.Position).Magnitude -- Get distance
      part.Transparency = math.clamp(distance, MinDistance, MaxDistance) / MaxDistance
      debounce = false
   end
end)

this should run the code only when it needs to run

I think there is a way to implement a table so this happens to all the barriers of this type

I had to change a capitalization error

local player = game.Players.LocalPlayer
local LastPosition = nil
local debounce = false

local MinDistance = 0  -- change the minimum distance
local MaxDistance = 50  -- change the maximum distance
local PartTable = {
    -- you add all the parts here
    workspace.Part1,
    workspace.Part2
}

game:GetService("RunService").Heartbeat:Connect(function()
   local character = player.Character or player.CharacterAdded:Wait()
   if character.PrimaryPart == nil then return end
   local humanoidP = character.PrimaryPart
   if humanoidP.Position ~= LastPosition and debounce == false then
      debounce = true
      LastPosition = humanoidP.Position
      for i = 1, #PartTable, 1 do
         local part = PartTable[i]
         local distance = (humanoidP.Position - part.Position).Magnitude -- Get distance
         part.Transparency = math.clamp(distance, MinDistance, MaxDistance) / MaxDistance
      end
      debounce = false
   end
end)

remember to separate everything in PartTable with commas

1 Like