Advertisement
PersonsadminTeam

SurfaceHit_Function

Jun 17th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.71 KB | None | 0 0
  1. getLeast = function(tab)
  2.     startnum = math.huge
  3.     ind = 0
  4.     if type(tab) == "table" then
  5.         for i, v in pairs(tab) do
  6.             if type(v) == "number" and v < startnum then
  7.                 startnum = v
  8.                 ind = i
  9.             end
  10.         end
  11.     end
  12.     return startnum ~= math.huge and startnum, ind or startnum == math.huge and "error"
  13. end
  14. --
  15. getSurfaceHit = function(obj, hitter)
  16.     SurfaceTable = {"FrontSurface", "BackSurface", "RightSurface", "LeftSurface", "TopSurface", "BottomSurface"}
  17.     local lengthx = obj.Size.X/2
  18.     local lengthy = obj.Size.Y/2
  19.     local lengthz = obj.Size.Z/2
  20.     local front = (obj.CFrame*(obj.CFrame:toObjectSpace(obj.CFrame).lookVector*lengthz))
  21.     local back = (obj.CFrame*(obj.CFrame:toObjectSpace(obj.CFrame).lookVector*-lengthz))
  22.     local right = (obj.CFrame*CFrame.Angles(0, math.rad(-90), 0))*(obj.CFrame:toObjectSpace(obj.CFrame).lookVector*lengthx)
  23.     local left = (obj.CFrame*CFrame.Angles(0, math.rad(90), 0))*(obj.CFrame:toObjectSpace(obj.CFrame).lookVector*lengthx)
  24.     local top = (obj.CFrame*CFrame.Angles(math.rad(90), 0, 0))*(obj.CFrame:toObjectSpace(obj.CFrame).lookVector*lengthy)
  25.     local bottom = (obj.CFrame*CFrame.Angles(math.rad(-90), 0, 0))*(obj.CFrame:toObjectSpace(obj.CFrame).lookVector*lengthy)
  26.     --
  27.     local num, index = getLeast({(hitter.Position-front).magnitude, (hitter.Position-back).magnitude, (hitter.Position-right).magnitude, (hitter.Position-left).magnitude, (hitter.Position-top).magnitude, (hitter.Position-bottom).magnitude})
  28.     print(num, index, SurfaceTable[index])
  29.     return SurfaceTable[index]
  30. end
  31. --
  32. for i, v in pairs(script.Parent:GetChildren()) do
  33.     if v:IsA("BasePart") then
  34.         v.Touched:connect(function(hit)
  35.             surface = getSurfaceHit(v, hit)
  36.             print(surface)
  37.             v[surface] = "Studs"
  38.         end)
  39.     end
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement