How can I make the Players avatar go slightly transparent when their camera is close to the body?
Check if camera is close to humanoid root part then set local transparency modifier
I’m pretty sure the camera by default already but if you really needed to, you can use the heartbeat event to constantly check how close the camera is to the head and modify the character’s transparency.
Example (A localscript in StarterPlayerScripts)
local Threshold = --how close the camera needs to be in order for the character to be slightly transparent (in studs)
local ThresholdTransparency = --How transparent the character should be
local DefaultTransparency = 0--Change to what the default transparency should be when the distance between the camera and the character's head is not within the threshold
local player = game:GetService("Players").LocalPlayer
local RS = game:GetService("RunService")
local Camera = workspace.CurrentCamera
local IsWithin --This is to make sure the script doesn't have to loop through the character each loop when unnecessary
RS.Heartbeat:Connect(function()
local character = player.Character
local Head = character and character:FindFirstChild("Head")
if not Head then return end --returns if the player doesn't have a character or a head at that moment
local Distance = (Camera.CFrame.Position-Head.Position).Magnitude --Calculates the distance between the camera and the characters head
if Distance == 0 then return end -- returns if the distance is 0
if Distance <= Threshold and IsWithin ~= true then --The camera is within the threshold distance
IsWithin = true
for _,v in pairs(character) do --
if v:IsA("BasePart") then --Check if the instance is a basepart
v.Transparency = ThresholdTransparency
end
end
elseif IsWithin ~= false then
IsWithin = false
for _,v in pairs(character) do
if v:IsA("BasePart") then --Check if the instance is a basepart
v.Transparency = DefaultTransparency
end
end
end
end)
Tell me if you experience any issues with the code
I put it in a local script in StarterPlayer but i get this error:
Players.MasonX890.PlayerScripts.ANI_Transparency:23: invalid argument #1 to ‘pairs’ (table expected, got Instance) - Client
13:47:40.556 Stack Begin - Studio
13:47:40.556 Script ‘Players.MasonX890.PlayerScripts.ANI_Transparency’, Line 23 - Studio
13:47:40.556 Stack End - Studio
do character:GetChildren() in the pairs brackets (or :GetDescendants())
Camera:GetPropertyChangedSignal("CFrame"):Connect(function()
if (Camera.Position - HumanoidRootPart.Position).Magnitude < 1 then
for _, i in pairs(Character:GetChildren()) do
if i:IsA("MeshPart") then
i.Transparency = 0.5
end
end
end
end)
1 is a placeholder
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Head = Character:FindFirstChild("Head") or Character:WaitForChild("Head")
local Camera = Workspace.CurrentCamera
local function OnCameraChanged()
local Distance = (Head.Position - Camera.CFrame.Position).Magnitude
for _, Child in ipairs(Character:GetChildren()) do
if (Child.Name ~= "HumanoidRootPart") and Child:IsA("BasePart") then Child.Transparency = 1 - (Distance / 128) end
end
end
Camera:GetPropertyChangedSignal("CFrame"):Connect(OnCameraChanged)
Local script.
Camera Changed event is definitely the way to go, also as @zhenjie2003 said, i forgot to add “:GetDescendants()” cause i was in a rush