How to fix/simplify this Placement System Script? [closed]
Me and my friend have worked together on this placement system script for a while now as a school project.
I need to make this code shorter and more efficient/clean.
The whole code is here:
02 | local rs = game:WaitForChild( "ReplicatedStorage" ) |
03 | local base = game.Workspace.GrassTop |
04 | local placeablePart = rs:WaitForChild( "PlaceableObjects" ) |
05 | local Menu = game.Players.LocalPlayer.PlayerGui.PlacementSystem.ObjectMenu.ObjectList |
06 | local CurrentlyPlacing = false |
12 | local player = game.Players.LocalPlayer |
13 | local mouse = player:GetMouse() |
14 | local runService = game:GetService( "RunService" ) |
16 | function roundAToB(a,b) |
17 | return (math.floor((a/b)+ 0.5 )*b) |
20 | local modelPreview, modelPlace |
22 | modelRotate = mouse.KeyDown:connect( function (key) |
24 | rotation = rotation + 90 |
30 | Menu.Wall.MouseButton 1 Click:connect( function () |
31 | script.SelectedPart.Value = placeablePart.Wall |
32 | CurrentlyPlacing = true |
33 | cloneModel = script.SelectedPart.Value:clone() |
34 | cloneModel.Parent = game.Workspace |
40 | modelPreview = runService.RenderStepped:connect( function () |
41 | if CurrentlyPlacing = = true then |
42 | local modelCFrame = cloneModel:GetModelCFrame() |
43 | mouse.TargetFilter = cloneModel |
44 | local hit = mouse.Hit.p |
45 | cloneModel:SetPrimaryPartCFrame(CFrame.new(roundAToB(hit.X, 4 ), 1.5 ,roundAToB(hit.Z, 4 )) * CFrame.Angles( 0 , math.rad(rotation), 0 )) |
46 | if (cloneModel.PrimaryPart.Position.X > base.Position.X-base.Size.X/ 2 ) and (cloneModel.PrimaryPart.Position.Z > base.Position.Z-base.Size.Z/ 2 ) and (cloneModel.PrimaryPart.Position.X < base.Position.X+base.Size.X/ 2 ) and (cloneModel.PrimaryPart.Position.Z < base.Position.Z+base.Size.Z/ 2 ) then |
49 | cloneModel = script.SelectedPart.Value:clone() |
50 | cloneModel.Parent = game.Workspace |
51 | cloneModel:SetPrimaryPartCFrame(CFrame.new(roundAToB(hit.X, 4 ), 1.5 ,roundAToB(hit.Z, 4 )) * CFrame.Angles( 0 , math.rad(rotation), 0 )) |
53 | for i,v in pairs (cloneModel:GetChildren()) do |
54 | if v:IsA( 'BasePart' ) then |
61 | for i,v in pairs (cloneModel:GetChildren()) do |
62 | if v:IsA( 'BasePart' ) then |
75 | modelPlace = mouse.Button 1 Down:connect( function () |
76 | if CurrentlyPlacing = = true then |
77 | if canPlace = = true then |
78 | local new = cloneModel:clone() |
79 | new.Parent = workspace.Tycoons.TycoonTemplate.PurchasedObjects |
80 | for i,v in pairs (new:GetChildren()) do |
81 | if v:IsA( 'BasePart' ) then |
85 | elseif v.Name = = "Arrow" then |
88 | elseif v.Name = = "Drop" then |
95 | script.CurrentlyPlacing.Value = false |