How would i make a window that can be moved, similar to a windows program with GUI?

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!

2 Likes

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.

2 Likes

Ok ill try this, Thanks mans!
ill report back to you if it works or not

1 Like

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

1 Like

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)
2 Likes

It does not work… Did you test it for yourself? did it work for you?

1 Like

Yeah, it worked for me. Can you show me the hierarchy of your windows frame?

2 Likes

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:
image

1 Like

Did you change anything within the script?

1 Like

no i didnt.

char limitttttttttt

1 Like

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?

1 Like
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

1 Like

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)
2 Likes

OH IT WORKS NOW, MY BAR WAS JUST TOO SMALL
im so sorry, i just gotta turn off the grid size lool

1 Like

No problem, I’m glad it worked for you.

1 Like

Make sure to mark this as solved, unless you need help with something else regarding this.

1 Like