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

How to measure the Distance of the Part?

I want to know how to measure the distance the part flew/went for example, Bullet; when reached range <= 100 then do damage - 10. that was just an example of the thing I wanted to know...

1 answer

You would do something like this:

local Bullet = script.Parent --Change this to the path of the bullet
local Active = true
coroutine.resume(coroutine.create(function()
	local StartPos = Bullet.Position
	while true do
		if (Bullet.Position - StartPos).magnitude > 100 then --If the distance between the StartPos and the Bullet is > 100...
			Active = false
			break
		end
		wait()
	end
end))
Bullet.Touched:connect(function(Obj)
	if Active then --If the bullet hasn't reached a range of > 100...
		--Do stuff
	elseif (not Active) then --If the bullet has reached a range of > 100...
		--Do stuff
	end
end)

EDIT This is a much simpler version that LightArceus suggested:

local Bullet = script.Parent --Change this to the path of the bullet
local StartPos = Bullet.Position
Bullet.Touched:connect(function(Obj)
	local BulletDist = (Bullet.Position - StartPos).magnitude
	if BulletDist > 100 then --If the bullet hasn't reached a range of > 100...
		--Do stuff
	elseif BulletDist <= 100 then --If the bullet has reached a range of > 100...
		--Do stuff
	end
end)

2 comments

0

Wouldn't it be more efficient to put the distance checker in the touched function? That way you wouldn't have to use a loop.

0

thnx for the help guys, but I want to check the distance more than once so as for more realistic damage

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