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
|
  |
| Joined: 11 Feb 2009 |
| Total Posts: 15828 |
|
|
| 25 Apr 2015 06:32 AM |
| you have to use .magnitude |
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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 |
|
|