Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- MODULE SCRIPT
- local IsServer = game:GetService("RunService"):IsServer()
- local Placement = {}
- Placement._index = Placement
- function Placement.New(surface)
- local self = setmetatable({}, Placement)
- -- Describes what part is the surface
- self.Surface = surface
- -- Objects
- if (IsServer) then
- self.Objects = surface.Objects
- else
- self.Objects = game:GetService("ReplicatedStorage"):WaitForChild("Remotes").StartPlacement:InvokeServer(surface)
- end
- -- Describes what face objects will be placed on
- self.PlacingSurface = Enum.NormalId.Top
- -- How big each side of 1 grid unit is. Must be a divisible of the placing surface X and Z
- self.UnitStep = 1
- return self
- end
- function Placement:CalcSurfaceConstraints()
- local SurfaceSize = self.Surface.Size
- -- Make a CFRame where LookVector = UpVector
- -- Done by using Object space to build a CFrame
- local Back = Vector3.new(0, -1, 0) -- Z Axis vector
- local Top = Vector3.new(0, 0, -1) -- Y Axis vector
- local Right = Vector3.new(-1, 0, 0) -- X Axis vector
- -- Convert Back, Top and Right to a World Space CFrame
- local CF = self.Surface.CFrame * CFrame.fromMatrix(-Back*SurfaceSize/2, Right, Top, Back)
- -- Uses Object space vectors to find the width and height of the placement surface in whole numbers
- local Size = Vector2.new((SurfaceSize * Right).magnitude, (SurfaceSize * Top).magnitude)
- return CF, Size
- end
- function Placement:CalcPlacement(Model, Pos, Rot)
- -- Uses other method to get info about the placing surface
- local CF, Size = Placement:CalcSurcafeConstraints()
- -- Rotates the model size to properly constrain to the surface
- local ModelSize = CFrame.fromEulerAnglesYXZ(0, Rot, 0) * Model.PrimaryPart.Size
- ModelSize = Vector3.new(math.abs(ModelSize.X), math.abs(ModelSize.Y), math.abs(ModelSize.Z))
- -- Get the model position relative to the surface CFrame
- local LPos = CF:PointToObjectSpace(Pos)
- -- Max distance model can be from surface center
- local Size2 = (Size - Vector2.new(ModelSize.X, ModelSize.Z))/2
- -- Constraint the model position so you can't drag it off the base.
- local X = math.clamp(LPos.X, -Size2.X, Size2.X)
- local Y = math.clamp(LPos.Y, -Size2.Y, Size2.Y)
- -- Create the placement CFrame and return the paramters
- return CF * CFrame.new(X, Y, -ModelSize.Y/2) * CFrame.Angles(-math.pi/2, Rot, 0)
- end
- return Placement
- -- LOCAL SCRIPT
- local Placement = require(game.ReplicatedStorage.PlacementModule)
- local placement = Placement.New(game.Workspace.LandPlots.LandPlot1.Base)
- local CF, Size = placement:CalcSurfaceConstraints()
- print(CF, Size)
- -- ERROR FROM LOCAL SCRIPT
- 21:15:38.298 - Players.ilostmyaccount678.PlayerGui.LocalScript:4: attempt to call a nil value
Advertisement
Add Comment
Please, Sign In to add comment