Advertisement
XZTablets

Placement Checking

Jul 19th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.31 KB | None | 0 0
  1. function InitiatePlacing(ItemBtn)
  2.     if false then
  3. --      script.Parent.ItemSettings.Visible = false
  4.     else
  5.         if game.ReplicatedStorage.HasItem:InvokeServer(ItemBtn.ItemId.Value) > 0 and ItemBtn.ItemId.Value > 0 and not Placing and UsingMouse then
  6.     --      script.Parent.Cancel.Visible = true
  7.             Placing = true
  8.        
  9.             if script.Parent.Hovering.Hover.Details.Value then
  10.                 script.Parent.Hovering.Hover.Close:Fire()
  11.             end
  12.            
  13.             GoodPlacement = false
  14.             if not InputService.GamepadEnabled then
  15.                 Mouse.Icon = "rbxassetid://209048202"
  16.             end
  17.            
  18.             game.ContextActionService:BindAction("PlaceItem",Place,false,Enum.KeyCode.ButtonR2)
  19.                    
  20.             script.Parent.YYCenter.ObjectPlacement.Visible = true
  21.     --      script.Parent.Undo.Visible = true
  22.    
  23.     --      script.Parent.Place.Visible = (InputService.GamepadEnabled or (InputService.TouchEnabled and not InputService.MouseEnabled))           
  24.                    
  25.             if Mouse then
  26.                 Mouse.TargetFilter = workspace.Tycoons
  27.             end
  28.             script.Parent.YYLeft.Inventory.Visible = false
  29.            
  30.            
  31.             if game:GetService("UserInputService").TouchEnabled then
  32.                 game.Players.LocalPlayer.PlayerGui.TouchGui.TouchControlFrame.Visible = true
  33.             end        
  34.            
  35.             local toClone = getItemById(ItemBtn.ItemId.Value)
  36.             if toClone then
  37.                 Visual = toClone:clone()
  38.            
  39.                 local LocalItemTag = Instance.new("BoolValue")
  40.                 LocalItemTag.Name = "LocalItem"
  41.                 LocalItemTag.Parent = Visual           
  42.            
  43.                 Visual.Parent = GetTycoon()
  44.  
  45.                 if Visual then
  46.             --      script.Parent.CurrentlyPlacing.Thumbnail.Image = "rbxassetid://" .. Visual.ThumbnailId.Value
  47. --                  script.Parent.CurrentlyPlacing.Count.Text = "x" .. game.ReplicatedStorage.HasItem:InvokeServer(Visual.ItemId.Value)
  48.                     script.Parent.YYRight.Placement.Image.Image = "rbxassetid://" .. Visual.ThumbnailId.Value
  49.                     local Amount = "x" .. game.ReplicatedStorage.HasItem:InvokeServer(Visual.ItemId.Value)
  50.                     script.Parent.YYRight.Placement.Amount.Text = Amount
  51.                     script.Parent.YYRight.Placement.AmountDark.Text = Amount
  52.                     local EndPosition = UDim2.new(1,-300,0.6,0)
  53.                     if not game:GetService("UserInputService").TouchEnabled then
  54.             --          script.Parent.CurrentlyPlacing:TweenPosition(EndPosition,Enum.EasingDirection.InOut,Enum.EasingStyle.Quart,0.5)
  55.                         script.Parent.YYRight.Placement:TweenPosition(UDim2.new(-0.235,0,0.6,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quart,0.5, true)
  56.                     end
  57.                 end
  58.                        
  59.                 for i, TycoonItem in pairs(GetTycoon():GetChildren()) do
  60.                     if game.ReplicatedStorage.Items:FindFirstChild(TycoonItem.Name) and TycoonItem:IsA("Model") then
  61.                         local SelBox = TycoonItem:FindFirstChild("SelectionBox")
  62.                         if SelBox then
  63.                             SelBox.Visible = true
  64.                             SelBox.Transparency = 0.6
  65.                         end
  66.                     end
  67.                 end
  68.                        
  69.                                
  70.                        
  71.                 for i,v in pairs(workspace.Boxes:GetChildren()) do
  72.                     v.Visible = true
  73.                 end
  74.                        
  75.                 Visual.Hitbox.Transparency = 0.6
  76.                 Visual.Hitbox.Reflectance = 0.2
  77.                        
  78.                 local bMesh = Instance.new("BlockMesh",Visual.Hitbox)
  79.                 bMesh.Scale = Vector3.new(1,1000,1)
  80.                        
  81.                 if Visual:FindFirstChild("SelectionBox") then
  82.                     Visual.SelectionBox.Visible = true
  83.                     Visual.SelectionBox.Transparency = 0
  84.                 end                
  85.                        
  86.                 for ii,vv in pairs(Visual.Model:GetChildren()) do
  87.                     if vv:IsA("BasePart") then
  88.                         vv.CanCollide = false
  89.                         vv.Anchored = true
  90.                         if vv.Name == "Colored" then
  91.                             vv.BrickColor = game.Players.LocalPlayer.TeamColor
  92.                         end
  93.                     end
  94.                 end
  95.                        
  96.                 local TycoonBase = GetTycoon().Base
  97.                 local TycoonTopLeft = TycoonBase.CFrame * CFrame.new(Vector3.new(TycoonBase.Size.x/2, 0, TycoonBase.Size.z/2))
  98.                        
  99.                 local ModelItemId = Instance.new("IntValue", Visual)
  100.                 ModelItemId.Name = "ItemId"
  101.                 ModelItemId.Value = ItemBtn.ItemId.Value
  102.                        
  103.                 local Hitbox = getItemById(ItemBtn.ItemId.Value).Hitbox:clone()
  104.                 Hitbox.Parent = Visual
  105.                 Visual.PrimaryPart = Hitbox
  106.                        
  107.                 Visual:SetPrimaryPartCFrame(TycoonTopLeft * CFrame.new(Vector3.new(-Hitbox.Size.x/2, Hitbox.Size.y/2, -Hitbox.Size.z/2)))
  108.  
  109.  
  110.                 local TycoonBaseSizeX, TycoonBaseSizeZ
  111.                 local HitboxSizeX, HitboxSizeZ
  112.                 local Hitboxes
  113.                 local NewVector
  114.                
  115.                 local LastPosition = CFrame.new()
  116.                
  117.                 local function VerifyPos()
  118.                     local ModelColliding   
  119.                     local function Argue()
  120.                         for i, Hitbox in pairs(Hitboxes) do
  121.                             if Hitbox.Name == "Hitbox" and not Hitbox:IsDescendantOf(Visual) then
  122.                                 return false
  123.                             end
  124.                         end
  125.                         local CheckRay = Ray.new(Visual.Hitbox.Position, Vector3.new(0,-500,0))
  126.                         local Hit = workspace:FindPartOnRayWithIgnoreList(CheckRay,{GetTycoon()})
  127.                         if Hit and Hit:FindFirstChild("Base") and Hit.Name == GetTycoon().Name then
  128.                             return true
  129.                         end
  130.                         return false
  131.                     end
  132.                            
  133.                     local ModelColliding = not Argue()
  134.                                    
  135.                     GoodPlacement = not ModelColliding
  136.                     Visual:SetPrimaryPartCFrame(NewVector * CFrame.Angles(0, math.rad(Rotation), 0))
  137.                                    
  138.                     if GoodPlacement then
  139.                         LastPosition = NewVector * CFrame.Angles(0, math.rad(Rotation), 0)
  140.                         Visual.Hitbox.BrickColor = BrickColor.new("Bright blue")
  141.                         Visual.SelectionBox.Color3 = Color3.new(0.0509804, 0.411765, 0.67451)
  142.                     else
  143.                         Visual.Hitbox.BrickColor = BrickColor.new("Really red")
  144.                         Visual.SelectionBox.Color3 = Color3.new(1,0,0)
  145.                     end
  146.                 end
  147.                
  148.                 local function CheckPos(xdisp,zdisp,placepos) -- placepos is Mouse.Hit.p on pc
  149.                     xdisp = xdisp or 0
  150.                     zdisp = zdisp or 0
  151.                     if Rotation % 180 == 0 then
  152.                         TycoonBaseSizeX, TycoonBaseSizeZ = TycoonBase.Size.x, TycoonBase.Size.z
  153.                         HitboxSizeX, HitboxSizeZ = Hitbox.Size.x, Hitbox.Size.z
  154.                     else
  155.                         TycoonBaseSizeX, TycoonBaseSizeZ = TycoonBase.Size.x, TycoonBase.Size.z
  156.                         HitboxSizeX, HitboxSizeZ = Hitbox.Size.z, Hitbox.Size.x
  157.                     end
  158.                            
  159.                     local TycoonTopLeft = TycoonBase.CFrame * CFrame.new(Vector3.new(TycoonBase.Size.x/2, 0, TycoonBase.Size.z/2))
  160.                    
  161.                     local BaseAngleX, BaseAngleY, BaseAngleZ = CFrame.new(TycoonBase.Position, (TycoonBase.CFrame*Vector3.new(1, 0, 0))):toEulerAnglesXYZ()
  162.                     local VectorRotation = CFrame.Angles(BaseAngleX, BaseAngleY, BaseAngleZ)
  163.                    
  164.                     local RoundByThreeX = RoundByThree(math.floor(placepos.x) + xdisp - math.ceil(TycoonTopLeft.x))
  165.                     local RoundByThreeZ = RoundByThree(math.floor(placepos.z) + zdisp - math.ceil(TycoonTopLeft.z))
  166.                            
  167.                     if RoundByThreeX < -TycoonBaseSizeX + HitboxSizeX then
  168.                         RoundByThreeX = RoundByThreeX + HitboxSizeX
  169.                     end
  170.                                
  171.                     if RoundByThreeZ < -TycoonBaseSizeZ + HitboxSizeZ then
  172.                         RoundByThreeZ = RoundByThreeZ + HitboxSizeZ
  173.                     end
  174.                                
  175.                     local VectorComponent1 = CFrame.new(Vector3.new(TycoonTopLeft.x, TycoonBase.Position.y, TycoonTopLeft.z))
  176.                     local VectorComponent2 = CFrame.new(Vector3.new(RoundByThreeX, TycoonBase.Size.y/2, RoundByThreeZ)) --*VectorRotation
  177.                     local VectorComponent3 = CFrame.new(Vector3.new(-HitboxSizeX/2, Hitbox.Size.y/2, -HitboxSizeZ/2))
  178.                                
  179.                     NewVector = VectorComponent1*VectorComponent2*VectorComponent3
  180.                                
  181.                                
  182.                     local RegionVectorMin = NewVector * CFrame.new(Vector3.new(HitboxSizeX/2, -Hitbox.Size.y/2, HitboxSizeZ/2))
  183.                     local RegionVectorMax = NewVector * CFrame.new(-Vector3.new(HitboxSizeX/2, -Hitbox.Size.y/2, HitboxSizeZ/2))
  184.                                
  185.                     Hitboxes = game.Workspace:FindPartsInRegion3(
  186.                         Region3.new(
  187.                             Vector3.new(
  188.                                 math.min(RegionVectorMin.x, RegionVectorMax.x),
  189.                                 math.min(RegionVectorMin.y, RegionVectorMax.y),
  190.                                 math.min(RegionVectorMin.z, RegionVectorMax.z)
  191.                             ) + Vector3.new(0.1, 0, 0.1),
  192.                             Vector3.new(
  193.                                 math.max(RegionVectorMin.x, RegionVectorMax.x),
  194.                                 math.max(RegionVectorMin.y, RegionVectorMax.y),
  195.                                 math.max(RegionVectorMin.z, RegionVectorMax.z)
  196.                             ) - Vector3.new(0.1, 0, 0.1)
  197.                         ), game.Workspace.Map, 100
  198.                     )
  199.                     return true
  200.                 end
  201.            
  202.                 while Placing do
  203.                     GetTycoon().Base.Grid.Transparency = 0.5
  204.                     if TycoonBase ~= nil then
  205.                         local UIS = game:GetService("UserInputService")
  206.                         if UIS.GamepadEnabled or (UIS.TouchEnabled and not UIS.MouseEnabled) then
  207.                             local LookVector = workspace.CurrentCamera.CoordinateFrame.lookVector
  208.                             local xCo = 1
  209.                             local zCo = 1
  210.                             if LookVector.x > 0 then
  211.                                 xCo = 2
  212.                             end
  213.                             if LookVector.z > 0 then
  214.                                 zCo = 2
  215.                             end
  216.                             NewVector = Vector3.new(LookVector.x * xCo, LookVector.y, LookVector.z * zCo)
  217.                             local Pos = Player.Character.HumanoidRootPart.Position + (NewVector * 20)
  218.                 --          local Pos = Player.Character.Torso.Position + (NewVector * 20)
  219.                             CheckPos(0,0,Pos)
  220.                             VerifyPos()                        
  221.                            
  222.                         elseif Mouse ~= nil and Mouse.Target ~= nil and Mouse.Target.Name == GetTycoon().Name and Mouse.Target:FindFirstChild("Base") then
  223.  
  224.                             CheckPos(0,0,Mouse.Hit.p)
  225.                             VerifyPos()
  226.                             --TransformModel(Visual:GetChildren(), Visual.PrimaryPart.CFrame, CFrame.new(VectorComponent3.p, VectorComponent3*CFrame.new(1, 0, 0)) + CFrame.new(LastPosition.p), true)
  227.  
  228.                         end
  229.                     end
  230.                     game:GetService("RunService").Heartbeat:wait()
  231.                 end
  232.                 if Mouse then
  233.                     Mouse.TargetFilter = nil
  234.                 end
  235.                 --RemoveItem:InvokeServer(ItemBtn.ItemId.Value, 1)
  236.                 GetTycoon().Base.Grid.Transparency = 1
  237.                 --Mouse.Icon = ""
  238.                 --game:GetService("UserInputService").MouseIconEnabled = false
  239.             end
  240.         end
  241.     end
  242. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement