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