DistanceFromCharacter() returning incorrect value

I’m having trouble with DistanceFromCharacter() simply because 90% of the time it will return the correct distance that a player is from a part, but sometimes, it will return an incorrect distance.

I’m using this to create a task system where when a player walks away from the task, they are removed. When this error occurs, the player is literally standing right next to the task, and it says they’re 20 units away

I am almost positive that this is not a problem with my code, this is literally one line.

The trigger position does not change.

Any advice?

if player:DistanceFromCharacter(script.Parent.BOX.Trigger.Position) > 10 then
    -- run the code I want
end

You could try to manually get the distance instead, like so:

local function DistanceFromCharacter(player, pos)
    return pcall(function()
        return (player.Character.Head.Position - pos).Magnitude
    end)
end

-- code in between

if DistanceFromCharacter(player, script.Parent.BOX.Trigger.Position) > 10 then
    -- run the code I want
end

If this doesn’t work, it would be appreciated if you send the full script.

I’ll give this a shot. Thanks!

1 Like

It is returning nil. Any help?

1 Like

Could I see your full script, please? I’m not entirely sure what could be the issue at the moment.