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

create a local script , and put it in starter player>player scripts, and put this code:

local player = game.Players.LocalPlayer

game:GetService("RunService").Heartbeat:Connect(function()
   If player.Character.PrimaryPart == nil then return end
   local humanoidP = player.Character.PrimaryPart
   local part = workspace.Part -- Put your part
   local distance = (humanoidP.Position - part.Position).Magnitude -- Get distance
   if distance < 20 and distance > 10 then
       part.Transparency = 0.75
   elseif distance < 10 and distance > 5 then
       part.Transparency = 0.25
   elseif distance < 5 then
       part.Transparency = 0
   else
       part.Transparency = 1
   end
end)
1 Like

I realized you did that already my bad

image

I got no Idea why this error pops up, I read the code and nothing seems to be wrong.

ik why, they forgot an “end” at the end

replace the last line with

    end
end)

Hi!

Sorry for my English, I use a translator

A little strange~ Aren’t there too many conditions? I suggest you take advantage of the wonders of mathematics! After all, the transparency parameter takes a number from 0 to 1. Only by calculating the ratio of the current distance and the maximum distance can we get the corresponding values.

local MinDistance = 0 
local MaxDistnace = 50
while wait() do
	workspace.Part.Transparency = 1 -(math.clamp((game.Players.LocalPlayer.Character.PrimaryPart.Position - workspace.Part.Position).Magnitude,MinDistance,MaxDistnace) / MaxDistnace-(0.1))
end

View my reply again :slight_smile:

that’s what I thought

I’m like “why did they add so many if statements / elseif statements / elses”

It is a large part, so I am making some tweaks to this code…

no one is controlling how you make your game?

I wanted a more smoother transition with the thing but, this is the closest to what I want for now!

if you want it to be smooth use @Landlord2107’s idea

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 = 1 - math.clamp(distance, MinDistance, MaxDistance) / MaxDistance
end)

I took @JustThigas’s script and added @Landlord2107’s math and combined the two

1 Like

Use TweenService

There is one missing parenthesis in there, more specifically in the comma at

math.clamp((distance, --<< Error is here

no if the player is constantly moving that isn’t good

I edited it, try now
should work perfectly

Yeah uhm, this happens… (Transparency becomes 0 because local scripts refuse to locate anything after Player.Character, probably a WaitForChild() is needed? ) (Yes, it is a texture.)

did you try printing the Transparency, you sure it’s not just the texture

I don’t use textures so I wouldn’t know

I just edited it so it definitely has the character every time

I don’t know why the code wouldn’t work

I just noticed that the code is working inverse

The transparency becomes higher the more you get closer, but not the other way around