Advertisement
Me_Hker

Bring Npc

Oct 14th, 2024 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.83 KB | None | 0 0
  1. -- Create ScreenGui and Frame
  2. local ScreenGui = Instance.new("ScreenGui")
  3. ScreenGui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
  4. ScreenGui.ResetOnSpawn = false -- Prevents the UI from resetting on player respawn
  5.  
  6. local Frame = Instance.new("Frame")
  7. local Dropdown = Instance.new("TextButton")
  8. local ToggleButton = Instance.new("TextButton")
  9. local CloseButton = Instance.new("TextButton") -- Close button
  10. local OpenButton = Instance.new("TextButton") -- Open button
  11. local DropdownFrame = Instance.new("ScrollingFrame") -- Use ScrollingFrame for dropdown options
  12. local UIS = game:GetService("UserInputService")
  13.  
  14. Frame.Parent = ScreenGui
  15.  
  16. -- Frame setup
  17. Frame.Size = UDim2.new(0.25, 0, 0.15, 0) -- Smaller frame
  18. Frame.Position = UDim2.new(0.5, 0, 0.5, 0)
  19. Frame.AnchorPoint = Vector2.new(0.5, 0.5)
  20.  
  21. -- Dropdown button setup (smaller)
  22. Dropdown.Parent = Frame
  23. Dropdown.Size = UDim2.new(0.7, 0, 0.4, 0) -- Smaller button
  24. Dropdown.Position = UDim2.new(0.15, 0, 0.1, 0)
  25. Dropdown.Text = "Select NPC"
  26. Dropdown.TextScaled = true
  27.  
  28. -- Toggle button setup (smaller)
  29. ToggleButton.Parent = Frame
  30. ToggleButton.Size = UDim2.new(0.7, 0, 0.4, 0) -- Smaller button
  31. ToggleButton.Position = UDim2.new(0.15, 0, 0.55, 0)
  32. ToggleButton.Text = "Start Bringing NPCs"
  33. ToggleButton.TextScaled = true
  34.  
  35. -- Close button setup (red)
  36. CloseButton.Parent = Frame
  37. CloseButton.Size = UDim2.new(0.15, 0, 0.15, 0) -- Small button
  38. CloseButton.Position = UDim2.new(0.85, 0, 0.05, 0) -- Top right corner
  39. CloseButton.Text = "Close"
  40. CloseButton.BackgroundColor3 = Color3.new(1, 0, 0) -- Red color
  41. CloseButton.TextScaled = true
  42.  
  43. -- Open button setup (green, smaller)
  44. OpenButton.Parent = ScreenGui
  45. OpenButton.Size = UDim2.new(0.05, 0, 0.05, 0) -- Smaller button
  46. OpenButton.Position = UDim2.new(0.9, 0, 0.9, 0) -- Bottom right corner
  47. OpenButton.Text = "Open"
  48. OpenButton.BackgroundColor3 = Color3.new(0, 1, 0) -- Green color
  49. OpenButton.TextScaled = true
  50. OpenButton.Visible = false -- Start as hidden
  51.  
  52. -- Dropdown Frame (ScrollingFrame)
  53. DropdownFrame.Parent = Frame
  54. DropdownFrame.Size = UDim2.new(1, 0, 0, 100) -- Visible size of the frame
  55. DropdownFrame.Position = UDim2.new(0, 0, 0.5, 0)
  56. DropdownFrame.Visible = false
  57. DropdownFrame.CanvasSize = UDim2.new(0, 0, 0, 0) -- Adjust dynamically based on content
  58. DropdownFrame.ScrollBarThickness = 10
  59. DropdownFrame.BackgroundTransparency = 0.5
  60. DropdownFrame.BorderSizePixel = 0
  61.  
  62. -- Dropdown list population
  63. local selectedNPC
  64. local function populateDropdown()
  65.     -- Clear existing buttons in the dropdown
  66.     for _, child in pairs(DropdownFrame:GetChildren()) do
  67.         if child:IsA("TextButton") then
  68.             child:Destroy()
  69.         end
  70.     end
  71.  
  72.     local uniqueNPCNames = {}
  73.  
  74.     -- Get unique NPC names from the workspace (excluding players)
  75.     for _, npc in pairs(game.Workspace:GetDescendants()) do
  76.         if npc:FindFirstChild("Humanoid") and not game.Players:FindFirstChild(npc.Name) then
  77.             if not uniqueNPCNames[npc.Name] then
  78.                 uniqueNPCNames[npc.Name] = true
  79.             end
  80.         end
  81.     end
  82.  
  83.     -- Create a button for each unique NPC name
  84.     local buttonHeight = 30 -- Set button height for consistency
  85.     local currentY = 0 -- Track Y position of buttons
  86.  
  87.     for npcName, _ in pairs(uniqueNPCNames) do
  88.         local npcButton = Instance.new("TextButton")
  89.         npcButton.Parent = DropdownFrame
  90.         npcButton.Size = UDim2.new(1, 0, 0, buttonHeight)
  91.         npcButton.Position = UDim2.new(0, 0, 0, currentY)
  92.         npcButton.Text = npcName
  93.         npcButton.TextScaled = true
  94.  
  95.         npcButton.MouseButton1Click:Connect(function()
  96.             selectedNPC = npcName
  97.             Dropdown.Text = npcName -- Set the dropdown text to the selected NPC
  98.             DropdownFrame.Visible = false -- Hide the dropdown after selection
  99.         end)
  100.  
  101.         currentY = currentY + buttonHeight -- Move to next button position
  102.     end
  103.    
  104.     -- Adjust CanvasSize to fit the total number of buttons
  105.     DropdownFrame.CanvasSize = UDim2.new(0, 0, 0, currentY)
  106. end
  107.  
  108. -- Toggle dropdown visibility
  109. Dropdown.MouseButton1Click:Connect(function()
  110.     DropdownFrame.Visible = not DropdownFrame.Visible
  111.     if DropdownFrame.Visible then
  112.         populateDropdown()
  113.     end
  114. end)
  115. -- Function to bring NPCs to player and anchor them
  116. local bringLoop = false
  117. local function bringNPCs()
  118.     while bringLoop do
  119.         if selectedNPC then
  120.             local npcFound = false -- Track if NPC is found
  121.             for _, npc in pairs(game.Workspace:GetDescendants()) do
  122.                 if npc.Name == selectedNPC and npc:FindFirstChild("Humanoid") then
  123.                     npcFound = true
  124.                     -- Bring NPC to 1 stud in front of the player every 0.2 seconds
  125.                     local targetPosition = game.Players.LocalPlayer.Character.HumanoidRootPart.Position + game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame.LookVector * 1
  126.                     npc:SetPrimaryPartCFrame(CFrame.new(targetPosition)) -- Move the NPC
  127.  
  128.                     -- Anchor the NPC to keep it in place
  129.                     if npc.PrimaryPart then
  130.                         npc.PrimaryPart.Anchored = true
  131.                     end
  132.  
  133.                     wait(0.2) -- Wait before moving the next NPC
  134.                 end
  135.             end
  136.  
  137.             -- If no NPCs were found, wait and check again for respawned NPCs
  138.             if not npcFound then
  139.                 wait(1)
  140.             end
  141.         end
  142.         wait(1) -- Continue checking in the loop
  143.     end
  144. end
  145.  
  146. -- Toggle bring NPCs
  147. ToggleButton.MouseButton1Click:Connect(function()
  148.     bringLoop = not bringLoop -- Toggle the bringLoop on/off
  149.     if bringLoop then
  150.         ToggleButton.Text = "Stop Bringing NPCs"
  151.         spawn(bringNPCs) -- Start bringing in a new thread
  152.     else
  153.         ToggleButton.Text = "Start Bringing NPCs"
  154.     end
  155. end)
  156. -- Close button to minimize the UI
  157. CloseButton.MouseButton1Click:Connect(function()
  158.     Frame.Visible = false
  159.     OpenButton.Visible = true -- Show open button
  160. end)
  161.  
  162. -- Open button to restore the UI
  163. OpenButton.MouseButton1Click:Connect(function()
  164.     Frame.Visible = true
  165.     OpenButton.Visible = false -- Hide open button
  166. end)
  167.  
  168. -- Create drag functionality for the Open button
  169. local draggingOpen, dragInputOpen, dragStartOpen, startPosOpen
  170. local function updateOpen(input)
  171.     local delta = input.Position - dragStartOpen
  172.     OpenButton.Position = UDim2.new(startPosOpen.X.Scale, startPosOpen.X.Offset + delta.X, startPosOpen.Y.Scale, startPosOpen.Y.Offset + delta.Y)
  173. end
  174.  
  175. OpenButton.InputBegan:Connect(function(input)
  176.     if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
  177.         draggingOpen = true
  178.         dragStartOpen = input.Position
  179.         startPosOpen = OpenButton.Position
  180.  
  181.         input.Changed:Connect(function()
  182.             if input.UserInputState == Enum.UserInputState.End then
  183.                 draggingOpen = false
  184.             end
  185.         end)
  186.     end
  187. end)
  188.  
  189. OpenButton.InputChanged:Connect(function(input)
  190.     if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
  191.         dragInputOpen = input
  192.     end
  193. end)
  194.  
  195. UIS.InputChanged:Connect(function(input)
  196.     if input == dragInputOpen and draggingOpen then
  197.         updateOpen(input)
  198.     end
  199. end)
  200.  
  201. -- Make frame draggable on both PC and mobile
  202. local dragging, dragInput, dragStart, startPos
  203. local function update(input)
  204.     local delta = input.Position - dragStart
  205.     Frame.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
  206. end
  207.  
  208. Frame.InputBegan:Connect(function(input)
  209.     if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
  210.         dragging = true
  211.         dragStart = input.Position
  212.         startPos = Frame.Position
  213.  
  214.         input.Changed:Connect(function()
  215.             if input.UserInputState == Enum.UserInputState.End then
  216.                 dragging = false
  217.             end
  218.         end)
  219.     end
  220. end)
  221.  
  222. Frame.InputChanged:Connect(function(input)
  223.     if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
  224.         dragInput = input
  225.     end
  226. end)
  227.  
  228. UIS.InputChanged:Connect(function(input)
  229.     if input == dragInput and dragging then
  230.         update(input)
  231.     end
  232. end)
  233.  
  234. -- Final adjustments to the ScrollingFrame for proper scrolling behavior
  235. DropdownFrame.ScrollBarThickness = 10
  236. DropdownFrame.VerticalScrollBarPosition = Enum.VerticalScrollBarPosition.Right
  237.  
  238. -- Set the background color of the frame for better visibility
  239. Frame.BackgroundColor3 = Color3.fromRGB(50, 50, 50) -- Dark gray background for the frame
  240. DropdownFrame.BackgroundColor3 = Color3.fromRGB(200, 200, 200) -- Light gray for dropdown options
  241.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement