A weird problem I have with DistanceFromCharacter

So I made a LocalScript to make parts be transparent when they are close to you. The problem is that the parts that are registered here always say they are 0 studs away from the character. Is there a way to fix this?

Local Script in StarterPlayerScripts:

local RunService = game:GetService("RunService")
local PlayerService = game:GetService("Players")

local Player = PlayerService.LocalPlayer

local FadeObj = {}

for i, obj in ipairs(workspace:GetDescendants()) do
	if obj:IsA("ObjectValue") and obj.Name == "_Distance" then
		table.insert(FadeObj, 1, obj.Parent)
	end
end

for i, part in ipairs(FadeObj) do
	local plrDistance = Player:DistanceFromCharacter(part.Position)
	
	function Update()
		print(plrDistance)
		if plrDistance < 10 then
			part.Transparency = 1
		else
			part.Transparency = 0
		end
	end
	
	RunService.Heartbeat:Connect(Update)
end

Curious, why do you call :DistanceFromCharacter only once per object, and you don’t update that variable dynamically?

1 Like

Wait so it only reads the distance once? Should I put it inside the function instead?

:DistanceFromCharacter returns a number once when called, it isn’t like a property that updates in realtime.

I might also take a guess in saying it’s returning 0 because the character has not yet loaded in the game

1 Like

Ok so now it works properly when i put it inside the function

1 Like

Just for clarification, using :DistanceFromCharacter returns the `float of exact studs away from said position. This is the equivalent of using

local distance = (character.Head - part.Position).Magnitude
1 Like