Guest User

Script

a guest
May 22nd, 2020
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.68 KB | None | 0 0
  1. -- MODULE SCRIPT
  2. local IsServer = game:GetService("RunService"):IsServer()
  3.  
  4. local Placement = {}
  5. Placement._index = Placement
  6.  
  7. function Placement.New(surface)
  8.     local self = setmetatable({}, Placement)
  9.     -- Describes what part is the surface
  10.     self.Surface = surface
  11.    
  12.     -- Objects
  13.     if (IsServer) then
  14.         self.Objects = surface.Objects
  15.     else   
  16.         self.Objects = game:GetService("ReplicatedStorage"):WaitForChild("Remotes").StartPlacement:InvokeServer(surface)
  17.     end
  18.     -- Describes what face objects will be placed on
  19.     self.PlacingSurface = Enum.NormalId.Top
  20.     -- How big each side of 1 grid unit is. Must be a divisible of the placing surface X and Z
  21.     self.UnitStep = 1
  22.     return self
  23. end
  24.    
  25. function Placement:CalcSurfaceConstraints()
  26.     local SurfaceSize = self.Surface.Size
  27.    
  28.     -- Make a CFRame where LookVector = UpVector
  29.     -- Done by using Object space to build a CFrame
  30.    
  31.     local Back = Vector3.new(0, -1, 0) -- Z Axis vector
  32.     local Top = Vector3.new(0, 0, -1) -- Y Axis vector
  33.     local Right = Vector3.new(-1, 0, 0) -- X Axis vector
  34.    
  35.     -- Convert Back, Top and Right to a World Space CFrame
  36.     local CF = self.Surface.CFrame * CFrame.fromMatrix(-Back*SurfaceSize/2, Right, Top, Back)  
  37.     -- Uses Object space vectors to find the width and height of the placement surface in whole numbers
  38.     local Size = Vector2.new((SurfaceSize * Right).magnitude, (SurfaceSize * Top).magnitude)
  39.    
  40.     return CF, Size
  41. end
  42.  
  43. function Placement:CalcPlacement(Model, Pos, Rot)
  44.     -- Uses other method to get info about the placing surface
  45.     local CF, Size = Placement:CalcSurcafeConstraints()
  46.     -- Rotates the model size to properly constrain to the surface
  47.     local ModelSize = CFrame.fromEulerAnglesYXZ(0, Rot, 0) * Model.PrimaryPart.Size
  48.     ModelSize = Vector3.new(math.abs(ModelSize.X), math.abs(ModelSize.Y), math.abs(ModelSize.Z))
  49.     -- Get the model position relative to the surface CFrame
  50.     local LPos = CF:PointToObjectSpace(Pos)
  51.     -- Max distance model can be from surface center
  52.     local Size2 = (Size - Vector2.new(ModelSize.X, ModelSize.Z))/2
  53.     -- Constraint the model position so you can't drag it off the base.
  54.     local X = math.clamp(LPos.X, -Size2.X, Size2.X)
  55.     local Y = math.clamp(LPos.Y, -Size2.Y, Size2.Y)
  56.     -- Create the placement CFrame and return the paramters
  57.     return CF * CFrame.new(X, Y, -ModelSize.Y/2) * CFrame.Angles(-math.pi/2, Rot, 0)
  58. end
  59.  
  60. return Placement
  61.  
  62. -- LOCAL SCRIPT
  63. local Placement = require(game.ReplicatedStorage.PlacementModule)
  64. local placement = Placement.New(game.Workspace.LandPlots.LandPlot1.Base)
  65.  
  66. local CF, Size = placement:CalcSurfaceConstraints()
  67. print(CF, Size)
  68.  
  69. -- ERROR FROM LOCAL SCRIPT
  70.   21:15:38.298 - Players.ilostmyaccount678.PlayerGui.LocalScript:4: attempt to call a nil value
Advertisement
Add Comment
Please, Sign In to add comment