Advertisement
aesnike

GUI

Oct 26th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. local player = game.Players.LocalPlayer
  2. local gui = nil
  3. local mainFrame = nil
  4. local closeButton = nil
  5.  
  6. -- Function to create the GUI
  7. local function createGui()
  8. -- Check if GUI already exists and destroy it
  9. if gui then
  10. gui:Destroy()
  11. end
  12.  
  13. local ScreenGui = Instance.new("ScreenGui")
  14. local MainFrame = Instance.new("Frame")
  15. local CloseButton = Instance.new("TextButton")
  16.  
  17. -- Set up ScreenGui
  18. ScreenGui.Name = "PersistentGUI"
  19. ScreenGui.ResetOnSpawn = false
  20. ScreenGui.Parent = player.PlayerGui
  21.  
  22. -- Set up CloseButton
  23. CloseButton.Size = UDim2.new(0, 100, 0, 25)
  24. CloseButton.Position = UDim2.new(0.5, -50, 0, 10)
  25. CloseButton.BackgroundColor3 = Color3.fromRGB(100, 100, 100)
  26. CloseButton.Text = "Close"
  27. CloseButton.TextColor3 = Color3.fromRGB(255, 255, 255)
  28. CloseButton.Font = Enum.Font.SourceSansBold
  29. CloseButton.TextSize = 14
  30. CloseButton.Parent = ScreenGui
  31. CloseButton.ZIndex = 10
  32.  
  33. -- Set up MainFrame
  34. MainFrame.Size = UDim2.new(0.2, 0, 0.6, 0)
  35. MainFrame.Position = UDim2.new(0.5, -MainFrame.Size.X.Scale * 500, 0, 80) -- Increased Y offset to 80
  36. MainFrame.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
  37. MainFrame.BackgroundTransparency = 0.5
  38. MainFrame.BorderSizePixel = 0
  39. MainFrame.ClipsDescendants = true
  40. MainFrame.Visible = true
  41. MainFrame.ZIndex = 1
  42. MainFrame.Parent = ScreenGui
  43.  
  44. -- Create list layout for buttons
  45. local UIListLayout = Instance.new("UIListLayout")
  46. UIListLayout.Parent = MainFrame
  47. UIListLayout.HorizontalAlignment = Enum.HorizontalAlignment.Center
  48. UIListLayout.Padding = UDim.new(0, 20)
  49. UIListLayout.SortOrder = Enum.SortOrder.LayoutOrder
  50.  
  51. -- Function to create toggle buttons
  52. local function createToggleButton(number)
  53. local button = Instance.new("TextButton")
  54. button.Size = UDim2.new(0.7, 0, 0, 30)
  55. button.BackgroundColor3 = Color3.fromRGB(0, 0, 0) -- Start with black
  56. button.Text = "Function " .. number
  57. button.TextColor3 = Color3.fromRGB(255, 255, 255)
  58. button.Font = Enum.Font.SourceSansBold
  59. button.TextSize = 14
  60. button.LayoutOrder = number
  61. button.Parent = MainFrame
  62. button.ZIndex = 2
  63.  
  64. -- Add rounded corners to button
  65. local corner = Instance.new("UICorner")
  66. corner.CornerRadius = UDim.new(0.3, 0)
  67. corner.Parent = button
  68.  
  69. -- Toggle state
  70. local isEnabled = false
  71.  
  72. button.MouseButton1Click:Connect(function()
  73. isEnabled = not isEnabled
  74. if isEnabled then
  75. button.BackgroundColor3 = Color3.fromRGB(0, 0, 255) -- Blue when enabled
  76. else
  77. button.BackgroundColor3 = Color3.fromRGB(0, 0, 0) -- Black when disabled
  78. end
  79. -- Add your function logic here for each button
  80. end)
  81.  
  82. return button
  83. end
  84.  
  85. -- Create 5 buttons
  86. for i = 1, 5 do
  87. createToggleButton(i)
  88. end
  89.  
  90. -- Add padding at the top of the frame
  91. local UIPadding = Instance.new("UIPadding")
  92. UIPadding.Parent = MainFrame
  93. UIPadding.PaddingTop = UDim.new(0, 20)
  94.  
  95. -- Rounded corners for MainFrame
  96. local cornerFrame = Instance.new("UICorner")
  97. cornerFrame.CornerRadius = UDim.new(0.05, 0)
  98. cornerFrame.Parent = MainFrame
  99.  
  100. -- Rounded corners for CloseButton
  101. local cornerButton = Instance.new("UICorner")
  102. cornerButton.CornerRadius = UDim.new(0.3, 0)
  103. cornerButton.Parent = CloseButton
  104.  
  105. -- Store references to GUI elements
  106. gui = ScreenGui
  107. mainFrame = MainFrame
  108. closeButton = CloseButton
  109.  
  110. -- Function to update MainFrame position relative to CloseButton
  111. local function updateMainFramePosition()
  112. local buttonCenterX = CloseButton.AbsolutePosition.X + (CloseButton.AbsoluteSize.X / 2)
  113. local mainFrameHalfWidth = (MainFrame.Size.X.Scale * ScreenGui.AbsoluteSize.X) / 2
  114. MainFrame.Position = UDim2.new(0, buttonCenterX - mainFrameHalfWidth, 0, CloseButton.AbsolutePosition.Y + 80) -- Increased offset
  115. end
  116.  
  117. -- Function to toggle GUI visibility
  118. local function toggleCloseButton()
  119. MainFrame.Visible = not MainFrame.Visible
  120. CloseButton.Text = MainFrame.Visible and "Close" or "Open"
  121. if MainFrame.Visible then
  122. updateMainFramePosition()
  123. end
  124. end
  125.  
  126. -- Make both CloseButton and MainFrame draggable
  127. local function makeDraggable(gui)
  128. local dragging = false
  129. local dragStart = nil
  130. local startPos = nil
  131. local dragInput = nil
  132. local startPos = nil
  133.  
  134. gui.InputBegan:Connect(function(input)
  135. if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
  136. dragging = true
  137. dragStart = input.Position
  138. startPos = gui.Position
  139.  
  140. input.Changed:Connect(function()
  141. if input.UserInputState == Enum.UserInputState.End then
  142. dragging = false
  143. end
  144. end)
  145. end
  146. end)
  147.  
  148. gui.InputChanged:Connect(function(input)
  149. if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
  150. dragInput = input
  151. end
  152. end)
  153.  
  154. game:GetService("UserInputService").InputChanged:Connect(function(input)
  155. if input == dragInput and dragging then
  156. local delta = input.Position - dragStart
  157. if gui == CloseButton then
  158. local newY = math.clamp(startPos.Y.Offset + delta.Y, 10, 50)
  159. gui.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, 0, newY)
  160. if MainFrame.Visible then
  161. updateMainFramePosition()
  162. end
  163. else
  164. gui.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X,
  165. startPos.Y.Scale, startPos.Y.Offset + delta.Y)
  166. end
  167. end
  168. end)
  169. end
  170.  
  171. -- Make both elements draggable
  172. makeDraggable(CloseButton)
  173. makeDraggable(MainFrame)
  174.  
  175. -- Connect toggle function
  176. CloseButton.MouseButton1Click:Connect(toggleCloseButton)
  177.  
  178. -- Initial position update
  179. updateMainFramePosition()
  180.  
  181. -- Handle window resize
  182. game:GetService("RunService").RenderStepped:Connect(function()
  183. if MainFrame.Visible then
  184. updateMainFramePosition()
  185. end
  186. end)
  187. end
  188.  
  189. -- Create initial GUI
  190. createGui()
  191.  
  192. -- Handle character respawning
  193. player.CharacterAdded:Connect(function()
  194. if not gui or not gui.Parent then
  195. createGui()
  196. end
  197. end)
  198.  
  199. -- Handle player removal
  200. player.AncestryChanged:Connect(function(_, parent)
  201. if not parent then
  202. gui:Destroy()
  203. end
  204. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement