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...
COMMUNITY QUESTION
How to measure the Distance of the Part?
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)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
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.
thnx for the help guys, but I want to check the distance more than once so as for more realistic damage