I would like to make a windows-styled program window, that you can drag by clicking the top part.
I only need a script to drag the window, i can make all the other stuff.
If anyone can help, Thanks!
I would like to make a windows-styled program window, that you can drag by clicking the top part.
I only need a script to drag the window, i can make all the other stuff.
If anyone can help, Thanks!
Here is a script that will allow you to drag a frame, button, etc that is a gui:
local UserInputService = game:GetService("UserInputService")
local function enableDragging(gui)
local dragging
local dragStartPos
local startPos
local function updateDrag(input)
local delta = input.Position - dragStartPos
gui.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
end
gui.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = true
dragStartPos = input.Position
startPos = gui.Position
input.Changed:Connect(function()
if input.UserInputState == Enum.UserInputState.End then
dragging = false
end
end)
end
end)
gui.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement and dragging then
updateDrag(input)
end
end)
gui.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = false
end
end)
end
-- Example usage:
local gui = script.Parent -- Change this to your GUI frame
enableDragging(gui)
You may need to adjust certain parts of the script to fit your needs, but it should do what you need. Make sure to place this as a parent of the windows frame.
Ok ill try this, Thanks mans!
ill report back to you if it works or not
Yeah it works! One more thing, is there a way you can add a variable boolean that will toggle making it snap? kinda as if there was a grid? its fine if you dont want to do this mans
Here is an update to the script I provided that will move the frame as if it is on a grid (adjust the gridSize
variable as needed):
local UserInputService = game:GetService("UserInputService")
-- Adjust these values as needed
local gridSize = 50 -- Size of the grid
local snapEnabled = true -- Whether snapping is enabled
local function enableDragging(gui)
local dragging
local dragStartPos
local startPos
local function updateDrag(input)
local delta = input.Position - dragStartPos
local newPos = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
if snapEnabled then
-- Snap to grid
newPos = UDim2.new(
startPos.X.Scale,
math.floor((startPos.X.Offset + delta.X) / gridSize + 0.5) * gridSize,
startPos.Y.Scale,
math.floor((startPos.Y.Offset + delta.Y) / gridSize + 0.5) * gridSize
)
end
gui.Position = newPos
end
gui.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = true
dragStartPos = input.Position
startPos = gui.Position
input.Changed:Connect(function()
if input.UserInputState == Enum.UserInputState.End then
dragging = false
end
end)
end
end)
gui.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement and dragging then
updateDrag(input)
end
end)
gui.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = false
end
end)
end
local gui = script.Parent -- Change this to your GUI frame
enableDragging(gui)
It does not work… Did you test it for yourself? did it work for you?
Yeah, it worked for me. Can you show me the hierarchy of your windows frame?
Ok, Also i tested it with the snap bool off, and it still works, but with the snap bool it on wont. heres the path if thats what ur asking:
Did you change anything within the script?
no i didnt.
char limitttttttttt
Can you copy and paste the script that you put into the local script? If I can see it, I can maybe tell what is wrong. One more thing, are there any errors in the output?
local UserInputService = game:GetService("UserInputService")
-- Adjust these values as needed
local gridSize = 50 -- Size of the grid
local snapEnabled = false -- Whether snapping is enabled
local function enableDragging(gui)
local dragging
local dragStartPos
local startPos
local function updateDrag(input)
local delta = input.Position - dragStartPos
local newPos = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
if snapEnabled then
-- Snap to grid
newPos = UDim2.new(
startPos.X.Scale,
math.floor((startPos.X.Offset + delta.X) / gridSize + 0.5) * gridSize,
startPos.Y.Scale,
math.floor((startPos.Y.Offset + delta.Y) / gridSize + 0.5) * gridSize
)
end
gui.Position = newPos
end
gui.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = true
dragStartPos = input.Position
startPos = gui.Position
input.Changed:Connect(function()
if input.UserInputState == Enum.UserInputState.End then
dragging = false
end
end)
end
end)
gui.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement and dragging then
updateDrag(input)
end
end)
gui.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = false
end
end)
end
local gui = script.Parent -- Change this to your GUI frame
enableDragging(gui)
I turned off snap grid, but it didnt work before
I’m not sure why, but the script you gave me didn’t work with the grid, but mine did. Try recopying this script into your local script, replace everything else within it, then tell me if it works or not.
local UserInputService = game:GetService("UserInputService")
-- Adjust these values as needed
local gridSize = 50 -- Size of the grid
local snapEnabled = true -- Whether snapping is enabled
local function enableDragging(gui)
local dragging
local dragStartPos
local startPos
local function updateDrag(input)
local delta = input.Position - dragStartPos
local newPos = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
if snapEnabled then
-- Snap to grid
newPos = UDim2.new(
startPos.X.Scale,
math.floor((startPos.X.Offset + delta.X) / gridSize + 0.5) * gridSize,
startPos.Y.Scale,
math.floor((startPos.Y.Offset + delta.Y) / gridSize + 0.5) * gridSize
)
end
gui.Position = newPos
end
gui.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = true
dragStartPos = input.Position
startPos = gui.Position
input.Changed:Connect(function()
if input.UserInputState == Enum.UserInputState.End then
dragging = false
end
end)
end
end)
gui.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement and dragging then
updateDrag(input)
end
end)
gui.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = false
end
end)
end
local gui = script.Parent -- Change this to your GUI frame
enableDragging(gui)
OH IT WORKS NOW, MY BAR WAS JUST TOO SMALL
im so sorry, i just gotta turn off the grid size lool
No problem, I’m glad it worked for you.
Make sure to mark this as solved, unless you need help with something else regarding this.