Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I get distance inbetween parts?

I am trying to get the distance between a player's HumanoidRootPart and the main fireplace part so that I can make it so that you can light the torch up it in a certain range. I've been trying lots of methods recently for example Roblox's method.

local magnitude = (part1.Position - part2.Position).magnitude
while true do
	if magnitude<5 then
		--- code ---
	end
end

I've replaced part1 and part2 with my parts and it did not work. I've also tried a different method posted on this website 3 years ago.

local player = game.Players.Player1
local radius = 5

if player:DistanceFromCharacter(part) < radius then
    print('player is within radius')
end

I've also replaced part with my part and it did not work either. Is this a problem or are my scripting skills still under a rock?

1 comment

0

The distance is determine when you define the magnitude variable, meaning that it doesn't update as your character moves. You need to move magnitude(your line 1) into the while true do loop so that it updates. Also, you need to add a wait into your loops, so roblox doesn't freeze. You can do so by typing wait()

1 answer

while wait(1) do
local magnitude = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - game.Workspace:FindFirstChild("Baseplate").Position).magnitude
    if magnitude<5 then
        print("HOLY COW!!!")
    end
end

--distance between character and baseplate local script

2 comments

0

It didnt work

0

can you dont

This discussion is preserved as it was. New answers, comments, and votes are closed.