generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Creating a part which points at the nearest Torso

Previous Thread :: Next Thread 
fret13103 is not online. fret13103
Joined: 15 Mar 2010
Total Posts: 881
25 Apr 2015 06:26 AM
Wall of Code coming:
I've got it split into 3 functions(One is global because I want to use it in another script without copy pasting it over and over)

local range = 30

function aimAtTorso(part)
if part ~= nil and _G.GetDistance(part, script.Parent) < range then
script.Parent.CFrame = CFrame.new(Vector3.new(script.Parent.Position),Vector3.new(part.Position))
end
end

function closestTorso()
for _, v in pairs(workspace:GetChildren()) do
if v:isA("Model") then
local Torso = v:findFirstChild("Torso")
if Torso then
return Torso
end
end
end
end

_G.GetDistance = function(pos1, pos2)
local position1 = pos1.Position
local position2 = pos2.Position
local Distance = math.sqrt((pos1.Position * pos1.Position + pos2.Position * pos2.Position))
print(Distance)
return Distance
end

while true do
aimAtTorso(closestTorso())
wait(.1)
end

Here's the error: Workspace.Pointer.Script:23: bad argument #1 to 'sqrt' (number expected, got userdata)

Report Abuse
WishNite is not online. WishNite
Joined: 11 Feb 2009
Total Posts: 15828
25 Apr 2015 06:32 AM
you have to use .magnitude
Report Abuse
TheDeveloping is not online. TheDeveloping
Joined: 22 Jun 2013
Total Posts: 13
25 Apr 2015 06:35 AM
On line 23 you are attempting to multiply 2 vector3 values.

I think the correct formula for Distance is

math.sqrt((position2.x-position1.x)^2,(position2.y-position1.y)^2,(position2.z-position1.z)^2)

Droy || Graphic Desiger || Founder of Metados
Report Abuse
WishNite is not online. WishNite
Joined: 11 Feb 2009
Total Posts: 15828
25 Apr 2015 06:43 AM
or you can use magnitude, which is the same equation essentially ._.
Report Abuse
fret13103 is not online. fret13103
Joined: 15 Mar 2010
Total Posts: 881
25 Apr 2015 06:54 AM
Well, I had some Tea, and rewrote it, Yeah. I was using math.sqrt wrong, thanks for the help, I'll research .magnitude.
Report Abuse
lampwnage121 is not online. lampwnage121
Joined: 20 Oct 2012
Total Posts: 4285
25 Apr 2015 06:56 AM
Is no one going to comment how his closestTorso function doesn't actually get the closest torso but rather the first one in the workspace?
Report Abuse
fret13103 is not online. fret13103
Joined: 15 Mar 2010
Total Posts: 881
25 Apr 2015 07:04 AM
lol, just checked here again and saw the post above, fixed that too.
Report Abuse
fret13103 is not online. fret13103
Joined: 15 Mar 2010
Total Posts: 881
25 Apr 2015 07:07 AM
function closestTorso()
local closesttorso = nil
for _, v in pairs(workspace:GetChildren()) do
if v:isA("Model") then
local Torso = v:findFirstChild("Torso")
if Torso then
if closesttorso == nil then closesttorso = Torso elseif _G.GetDistance(closesttorso, script.Parent) > _G.GetDistance(Torso, script.Parent) then closesttorso = Torso end
return closesttorso
end
end
end
end

--Should work
Report Abuse
fret13103 is not online. fret13103
Joined: 15 Mar 2010
Total Posts: 881
25 Apr 2015 07:12 AM
function closestTorso()
local closesttorso = nil
for _, v in pairs(workspace:GetChildren()) do
if v:isA("Model") and v:findFirstChild("Torso") then
local VPosition = v.Position
local Torso = v:findFirstChild("Torso")
if closesttorso == nil then
closesttorso = Torso
elseif _G.GetDistance(VPosition, script.Parent) < _G.GetDistance(closesttorso, script.Parent) then
closesttorso = Torso
end
end
end
return closesttorso
end

--corrected it
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image