How would i turn input.Position into scale?

I need to make draggable cards (well it’s not a card game) for my loadout system. DataStore and gui setup function are all done and i’ll need to make the drag and drop system.

However the issue is: input.Position from UserInputService uses vector2 as i’m forced to use offset instead the scale i wanted in UDim2. Here’s the vid i did watch How to Move a GUI on the Screen (Drag) - Roblox Studio Tutorial - YouTube

Though the x / viewportSize.X does not work for this as i searched from google…

Here’s the code:

local function updateInput(obj,input)
	local delta = input.Position - dragstart
	local curGui = clickable[top]

	local pos = UDim2.new(curGui.startpos.X.Scale + delta.X,curGui.startpos.X.Offset,curGui.startpos.Y.Scale + delta.Y,curGui.startpos.Y.Offset)
	game:GetService('TweenService'):Create(obj,TweenInfo.new(dragspeed),{Position = pos}):Play()
end

other lines:

UIS.InputBegan:Connect(function(input,gp)
	if not gp and (input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch) then
		dragstart = input.Position
		local pos = input.Position
		local under = PlayerGui:GetGuiObjectsAtPosition(pos.X, pos.Y)
		for a,obj in pairs(under) do
			local stop = false
			for i,v in pairs(clickable) do
				if tostring(obj) ~= tostring(i) then
					top = nil
				else
					top = under[a]
					clickable[top].startpos = top.Position
					stop = true
				end
			end
			if stop then
				break
			end
		end
		--if clickable[top].bool ~= nil then
			--clickable[top].startpos = top.Position
		--end
	end
end)

UIS.InputChanged:Connect(function(input)
	if (input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch)and isMouseButtonDown(Enum.UserInputType.MouseButton1) and top ~= nil then
		updateInput(top,input)
	end
end)

A Vector2 can be turned into scale values for UDim2 using the ScreenGui’s AbsoluteSize property.

local Screen_Gui = script.Parent -- or whatever. Replace this line with the correct path to the ScreenGui if needed.
local Mouse_Pos = input.Position / Screen_Gui.AbsoluteSize

-- Say if the mouse position is at 320 x 453, and the resolution is 1600 x 1200...

-- Values are now: 0.2 x 0.3775 for UDim2's scales.

You may have to compensate the Y-Value of the ScreenGui’s AbsoluteSize for the top bar scaling down all ScreenGuis that have IgnoreGuiInset disabled. The top bar is 36 pixels high.

2 Likes

Thanks! It worked, now i just need to do the collisions.

image

local function updateInput(obj,input)
	local inputpos = Vector2.new(input.Position.X / MainCards.AbsoluteSize.X,input.Position.Y / MainCards.AbsoluteSize.Y)
	local delta = inputpos - dragstart
	local curGui = clickable[top]

	local pos = UDim2.new(curGui.startpos.X.Scale + delta.X,curGui.startpos.X.Offset,curGui.startpos.Y.Scale + delta.Y,curGui.startpos.Y.Offset)
	game:GetService('TweenService'):Create(obj,TweenInfo.new(dragspeed),{Position = pos}):Play()
end
1 Like