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
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.
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
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?
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()
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
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”.