Distance from HumanoidRootPart and part?

  1. What do you want to achieve? Keep it simple and clear!
    I’m wondering how you get the distance between the player’s HumanoidRootPart and a part

  2. What is the issue? Include screenshots / videos if possible!
    I don’t how to do this. I’ve tried but somehow it’s not working.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    solutions work, but mine doesnt.

Basically I’m trying to get the distance from the player’s HumanoidRootPart and a part. I tried getting the distance by comparing their positions by Vector3 to see if the player’s root is less than or equal to a specific distance from a part, however I get the error “attempt to compare Vector”.

Anyways, I did a bit of research and found that the reason. Now I’m using magnitude to get the distance, however I can get the distance but the distance is not right.

For example. the trunks transparency = 1, even though the distance is not less that 10.
Output prints roughly 118 for magnitude.

local trunk = workspace.Tree.Trunk

if character then
  HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
  local Position = HumanoidRootPart.Position

  local magnitude = (trunk.Position - Position).Magnitude
    print(magnitude)
     if magnitude < 10 then
         trunk.Transparency = 1
1 Like
1 Like

That still doesn’t work. Is there any way to use GetDistanceFromCharacter from the humanoidrootpart?

1 Like

Tell me the code you used

Working code:

Player:GetDistanceFromCharacter(vector3)
1 Like

I used this code, however does it require vector3. Can you use for example the position of the part, rather then vector. Also, it gets the distance between those two points, but then how you get a certain distance?

  if distance > 5 for example
-- do stuff
local TargetPlayerName = "dollychun"
local Target = game.Players[TargetPlayerName]
local distance = Target:GetDistanceFromCharacter(workspace.Part.Position)
if distance > 5 then
  --do something
end

This is my code right now. I used Player:DistanceFromCharacter, however the trees transparency doesn’t change if I’m close by. I get a warning instead.

local distance = player:DistanceFromCharacter(workspace.Tree.Trunk.Position)
    if distance < 5 then
		workspace.Tree.Trunk.Transparency = 1
	else
		warn()

try this:

local distance = player:DistanceFromCharacter(workspace.Tree.Trunk.Position)
print(distance)

Example 1:

local player = game.Players.LocalPlayer
while wait() do
   local dist = player:DistanceFromCharacter(workspace.MyPart.Position)

   if dist < 5 then
      -- code here
   end
end

Example 2:


local player = game.Players.LocalPlayer

while wait() do
	local char = player.Character or player.CharacterAdded:Wait()
	local dist = (char:WaitForChild("HumanoidRootPart").Position - workspace.MyPart.Position).magnitude)
	
	if dist < 5 then
		-- code
	end
end
1 Like

did you read this?

It’s prints out my distance. But nothing changes.
– 118.3054…

The OP asked for the distance between the HRP and a part, magnitude can do that as well, I just gave both examples.

1 Like

Try to approach the tree

(change script to this)

while wait() do
local distance = player:DistanceFromCharacter(workspace.Tree.Trunk.Position)
print(distance)
end
2 Likes

You need to use a loop and continuously check your distance between the part and your character. I gave 2 examples on this.

1 Like

I’m using a loop and continuously checking, but the loop stops after a few seconds. It’s like something’s interfering with the distance code. I’m also sending a remote event to the server to load the character, it probably is that.

Edit, Nevermind I got this to work. It was my remote event resetting the character, and that is why the loop stopped. Thankyou @ValuedSins and @dollychun for all your help. I never knew there was a function called “DistanceFromCharacter”.

2 Likes

Of course, always read the API before you start with your code if you’re confused.

1 Like