Advertisement
Scripting_King

Untitled

Dec 13th, 2024
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.49 KB | Source Code | 0 0
  1. -- Getting services from Roblox
  2. local Replicated = game:GetService("ReplicatedStorage")
  3. local UserInput = game:GetService("UserInputService")
  4. local PlayerService = game:GetService("Players")
  5. local RunService = game:GetService("RunService")
  6. local workspace = game:GetService("Workspace")
  7.  
  8. -- Define the server-side remote event for placing objects
  9. local PlaceServer = Replicated.PlacementSystem.Place
  10. -- Object list for a specific player (to store objects the player is placing)
  11. local Objects = Replicated.PlacementSystem:WaitForChild(game.Players.LocalPlayer.Name.."Bugs")
  12. -- Module for attaching 3D models to UI
  13. local module3d = require(Replicated.Modules.Module3D)
  14. -- Spring animation module (used for smooth transitions)
  15. local Spring = require(script.SpringAnim)
  16.  
  17. -- Define the local player and related objects
  18. local Player = PlayerService.LocalPlayer
  19. local Character = Player.Character or Player.CharacterAdded:Wait()
  20. local Mouse = Player:GetMouse()
  21.  
  22. -- Client status to track the placing and rotating state
  23. local ClientStatus = {
  24.     ['Rotating'] = false, -- Whether the player is rotating the object
  25.     ['Placing'] = false,  -- Whether the player is placing the object
  26. }
  27.  
  28. -- Button used to cancel the placement process (can be changed to another button if desired)
  29. local CancelButton = "X"
  30.  
  31. -- Listen for when an object is added or removed, and rerun the loop
  32. Objects.ChildAdded:Connect(function()
  33.     rerunLoop()
  34. end)
  35.  
  36. Objects.ChildRemoved:Connect(function()
  37.     rerunLoop()
  38. end)
  39.  
  40. -- Function to rerun the placement logic
  41. function rerunLoop()
  42.     ClientStatus.Placing = false -- Reset placing status
  43.  
  44.     -- Get the list of objects in the "Objects" folder and limit to 5 objects for display
  45.     local objects = Objects:GetChildren()
  46.     local numObjects = math.min(5, #objects)
  47.  
  48.     -- Loop through the objects and display them in the UI
  49.     for i = 1, 5 do
  50.         local frame = script.Parent.Frame:FindFirstChild("Frame" .. i)
  51.         if frame then
  52.             if i <= numObjects then
  53.                 local obj = objects[i]
  54.                 frame.Visible = true
  55.                 local Button = frame.ViewportFrame:FindFirstChild("Button")
  56.  
  57.                 -- Attach the 3D model of the object to the viewport
  58.                 local petModel3d = module3d:Attach3D(frame.ViewportFrame, obj:Clone())
  59.                 petModel3d:SetDepthMultiplier(1.6)
  60.                 petModel3d.Camera.FieldOfView = 5
  61.                 petModel3d.Visible = true
  62.  
  63.                 -- Rotation effect for the model
  64.                 RunService.RenderStepped:Connect(function()
  65.                     petModel3d:SetCFrame(CFrame.Angles(0, tick() % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10), 0, 0))
  66.                 end)
  67.  
  68.                 -- Button click logic to place the object
  69.                 Button.MouseButton1Click:Connect(function()
  70.                     if ClientStatus.Placing == false then
  71.                         ClientStatus.Placing = true
  72.                         script.Parent.Frame.Visible = false
  73.  
  74.                         -- Initialization for the object placement process
  75.                         local RotInt = 0
  76.                         local ObjectClone = obj:Clone()
  77.                         local CanPlace = false
  78.                         local OrginColor
  79.  
  80.                         -- Store the original color of the object
  81.                         if ObjectClone:IsA("Model") then
  82.                             OrginColor = ObjectClone.PrimaryPart.Color
  83.                         else
  84.                             OrginColor = ObjectClone.Color
  85.                         end
  86.  
  87.                         local Connection
  88.                         local Connection2
  89.  
  90.                         -- Place the object in the workspace initially
  91.                         ObjectClone.Parent = workspace
  92.                         if ObjectClone:IsA("Model") then
  93.                             for _, child in pairs(ObjectClone:GetChildren()) do
  94.                                 if child:IsA("BasePart") then
  95.                                     child.CanCollide = false
  96.                                 end
  97.                             end
  98.                         else
  99.                             ObjectClone.CanCollide = false
  100.                         end
  101.  
  102.                         -- Set the initial position of the object
  103.                         local CFrameOffset = CFrame.new(0, -2, -5)
  104.                         if ObjectClone:IsA("Model") then
  105.                             ObjectClone.PrimaryPart.CFrame = Character.HumanoidRootPart.CFrame:ToWorldSpace(CFrameOffset)
  106.                         else
  107.                             ObjectClone.CFrame = Character.HumanoidRootPart.CFrame:ToWorldSpace(CFrameOffset)
  108.                         end
  109.  
  110.                         -- Create a selection box to show potential placement
  111.                         local NewBox = Instance.new('SelectionBox', ObjectClone)
  112.                         NewBox.Adornee = ObjectClone
  113.                         NewBox.Color3 = Color3.fromRGB(113, 255, 61)
  114.                         NewBox.LineThickness = 0
  115.  
  116.                         -- Update the placement logic with raycasting
  117.                         Connection2 = RunService.RenderStepped:Connect(function()
  118.                             local MouseRay = Mouse.UnitRay
  119.                             local RayCast = Ray.new(MouseRay.Origin, MouseRay.Direction * 10000)
  120.                             local IgnoreIndex = {ObjectClone, Character}
  121.  
  122.                             local Hit, Pos = workspace:FindPartOnRayWithIgnoreList(RayCast, IgnoreIndex)
  123.  
  124.                             if Hit and ((Hit:IsA('Part') or Hit:IsA('UnionOperation')) and Hit:FindFirstChild("CanPlace")) and (Character.HumanoidRootPart.Position - (ObjectClone.PrimaryPart.Position)).magnitude <= 25 then
  125.                                 CanPlace = true
  126.                                 Spring.target(ObjectClone.PrimaryPart, 1, 2.5, {Transparency = 0, Color = OrginColor})
  127.                                 Spring.target(NewBox, 1, 2, {LineThickness = 0, Color3 = Color3.fromRGB(113, 255, 61)})
  128.                             else
  129.                                 CanPlace = false
  130.                                 Spring.target(NewBox, 1, 2.5, {LineThickness = .15, Color3 = Color3.fromRGB(255, 52, 55)})
  131.                                 Spring.target(ObjectClone.PrimaryPart, 1, 2, {Transparency = .5, Color = Color3.fromRGB(255, 57, 60)})
  132.                             end
  133.  
  134.                             -- Update the CFrame of the object based on mouse position and rotation
  135.                             local AnglesCFrame = CFrame.Angles(0, math.rad(RotInt), 0)
  136.                             local modelSize = ObjectClone:GetExtentsSize()
  137.                             local heightOffset = modelSize.Y / 2
  138.  
  139.                             local NewCFrame = CFrame.new(Pos.X, Pos.Y + heightOffset, Pos.Z)
  140.                             ObjectClone.PrimaryPart.CFrame = NewCFrame * AnglesCFrame
  141.                         end)
  142.  
  143.                         -- Function to handle input events (rotation and placement)
  144.                         local function InputBegan(Input)
  145.                             -- Handle rotation with 'R' key
  146.                             if Input.KeyCode == Enum.KeyCode.R then
  147.                                 ClientStatus.Rotating = true
  148.  
  149.                                 -- Rotate the object as long as the player holds the key
  150.                                 while ClientStatus.Rotating == true do
  151.                                     if ClientStatus.Placing then
  152.                                         RotInt = RotInt + 5 -- Increment rotation angle
  153.                                     end
  154.                                     task.wait()
  155.                                 end
  156.                             end
  157.  
  158.                             -- Handle placement with the left mouse button
  159.                             if Input.UserInputType == Enum.UserInputType.MouseButton1 then
  160.                                 if ClientStatus.Placing then
  161.                                     if CanPlace == true then
  162.                                         local SetCFrame = ObjectClone.PrimaryPart.CFrame
  163.                                         local Response = Replicated.PlacementSystem['Place']:InvokeServer(ObjectClone.Name, SetCFrame)
  164.  
  165.                                         if Response == true and Response ~= nil then
  166.                                             -- Clean up after placement
  167.                                             Connection:Disconnect()
  168.                                             Connection2:Disconnect()
  169.                                             ClientStatus.Placing = false
  170.                                             ObjectClone:Destroy()
  171.                                             script.Parent.Frame.Visible = true
  172.                                         end
  173.                                     end
  174.                                 end
  175.                             end
  176.  
  177.                             -- Cancel placement with the 'X' key
  178.                             if Input.KeyCode == Enum.KeyCode.X then
  179.                                 if ClientStatus.Placing then
  180.                                     -- Disconnect connections and clean up the object
  181.                                     if Connection then Connection:Disconnect() end
  182.                                     if Connection2 then Connection2:Disconnect() end
  183.  
  184.                                     if ObjectClone then
  185.                                         ObjectClone:Destroy()
  186.                                     end
  187.  
  188.                                     ClientStatus.Placing = false
  189.                                     script.Parent.Frame.Visible = true
  190.                                 end
  191.                             end
  192.                         end
  193.  
  194.                         -- Connect the input event for rotation and placement
  195.                         Connection = UserInput.InputBegan:Connect(InputBegan)
  196.  
  197.                         -- Listen for when rotation stops
  198.                         UserInput.InputEnded:Connect(function(Input)
  199.                             if Input.KeyCode == Enum.KeyCode.R then
  200.                                 ClientStatus.Rotating = false
  201.                             end
  202.                         end)
  203.                     end
  204.                 end)
  205.             else
  206.                 -- Hide the frame if no objects are available to display
  207.                 frame.Visible = false
  208.             end
  209.         end
  210.     end
  211. end
  212.  
  213. -- Call the rerunLoop function to initialize the placement system
  214. rerunLoop()
  215.  
Tags: #scripting
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement