Frame corner is setting at wrong position

So I am trying to make selection box like this:
image

But everytime I start holding my leftMouseButton and dragging, the top left corner is always in the left of my screen, not setting onto the position where I began dragging:

I also get no error in the output
My script:

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local userInputService = game:GetService("UserInputService")
local connectionInputBegan = nil
local connectionInputEnded = nil
local userInput = nil
local isActivated = false
local startMousePositionX = nil
local startMousePositionY = nil
local selectionFrame = script.Parent:WaitForChild("Frame")

local function moveMouse()
	if isActivated == false then return end
	if (Vector2.new(startMousePositionX, startMousePositionY) - Vector2.new(mouse.X,mouse.Y)).Magnitude > 10 then
		isMultipleSelecting = true
		selectionFrame.Visible = true
		selectionFrame.Position = UDim2.new(Vector2.new(startMousePositionX, startMousePositionY))
		selectionFrame.Size = UDim2.fromOffset(mouse.X, mouse.Y)
	end
end

local function inputEnded(input, processed)
	if userInput ~= input then return end
	userInput = nil
	isActivated = false
	connectionInputEnded:Disconnect()
	startMousePositionX = nil
	startMousePositionY = nil
	selectionFrame.Visible = false
	print("Deactivated")
end

local function inputBegan(input, processed)
	if userInput ~= nil then return end
	if processed == true then return end
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	startMousePositionX = mouse.X
	startMousePositionY = mouse.Y
	userInput = input
	isActivated = true
	connectionInputEnded = userInputService.InputEnded:Connect(inputEnded)
	print("Activated")
end

mouse.Move:Connect(moveMouse)
connectionInputBegan = userInputService.InputBegan:Connect(inputBegan)

I don’t think this works? Any attempts at printing it on my end return with {0,0}, {0,0}
Would selectionFrame.Position = UDim2.fromOffset(startMousePositionX, startMousePositionY) work instead?

2 Likes

@uglyburger0 is right. Also selectionFrame.Size = UDim2.fromOffset(mouse.X - startMousePositionX, mouse.Y - startMousePositionY) for the size

2 Likes

I have tried that at first, but it acted very strangely, the right-bottom corner is not on the mouse’s position and it somehows feels like its scailing instead of offsetting, given the more in the right on my screen I start to drag the bigger it already is:

I didnt notice this, it is working now, thanks to you both!

1 Like