Gui Size having issues with Position

I am trying to make a script when you hold click onto a button it will make a road that will be positioned and sized based on the mouse position. But there are some issues, When I size the GUI on the Y Axis it seems to work fine just doesnt point to the direction my mouse is because that is the wrong Axis, but when I put it to the X Axis it does this weird thing where only downwards will point to my mouse and the starter position is not locked onto the GUI.

LocalScript:

btn.MouseButton1Down:Connect(function()
			if not canDragRoads or btn.Parent.Parent.Roads:FindFirstChild("DRAGGING") then return end
			currentDraggingCity = btn.Name
			local roadClone = road:Clone()
			roadClone.Parent = btn.Parent.Parent.Roads
			roadClone.Name = "DRAGGING"
			task.spawn(function()
				while true do
					if not roadClone or roadClone.Name ~= "DRAGGING" then break end
					local mousePos = UserInputService:GetMouseLocation()
					local gui = btn.Parent.Parent
					local guiPos = gui.AbsolutePosition
					local cityPos = btn.CityName.AbsolutePosition
					local relX = cityPos.X - guiPos.X
					local relY = cityPos.Y - guiPos.Y
					local dist = (Vector2.new(mousePos.X, mousePos.Y) - Vector2.new(cityPos.X, cityPos.Y)).Magnitude
					roadClone.Position = UDim2.new(0.011, relX, -0.08, relY)
					roadClone.Size = UDim2.new(roadClone.Size.X.Scale, roadClone.Size.X.Offset, 0, dist)
					roadClone.Rotation = math.deg(math.atan2(mousePos.Y - cityPos.Y, mousePos.X - cityPos.X)) + 90
					task.wait()
				end
			end)
		end)

Here’s what happening:

" roadClone.Size = UDim2.new(roadClone.Size.X.Scale, roadClone.Size.X.Offset, 0, dist)" When I tested it on Y now it does the exact same but scale the wrong way, so sizing it in general wont work at all, does anyone know how I can fix this so it just gets sized in a single direction and not change the position?

maybe try cloning the gui every time it passes through the end

Well you see, my game is gonna have a lot of GUI and if it goes over 1000 it’s gonna start having some performance issues, there is gonna be a lot of roads so making it 1 gui per road is the best way to deal with that

The issue is that the anchor point seems to stay the same when changing the size. Since the issue occurs when moving the UI across the X-axis I’ll assume that the anchor point of the script is stationary at (X, 0)

You’ll need to dynamically adjust the anchor point, as well as the position and size and rotation, in order for it to be pinned to Seattle.

(I was gonna explain how to do it, but that is left as an exercise for the reader)

I have tried that but I still do not know how to fix this, this is so annoying I just wanna fix this now

I have been trying to solve this for so long now and whenever I just size it, it changes the position, I dont know how to make it size in a single face instead of changing the position of it, basically around the button. Ive tried multiple of things to fix this, nothing worked, anchor point does not work and don’t know how to not make it change position when sizing up

Yeah, you were right. It didn’t have anything to do with anchor points, sorry about that, just a bad guess on my end.

Either way, it’s easier to show than to explain with words.
Seattle.rbxl (67.2 KB)

Path to script: StarterGui → ScreenGui → LocalScript

This version isn’t flawless, but I’m a bit lazy. Also, sorry for the late response, I went to sleep

1 Like

So far I found out the problem of the position but now I just need it to size in a single direction instead of top and bottom

btn.MouseButton1Down:Connect(function()
			if not canDragRoads or btn.Parent.Parent.Roads:FindFirstChild("DRAGGING") then return end
			currentDraggingCity = btn.Name
			local roadClone = road:Clone()
			roadClone.Parent = btn.Parent.Parent.Roads
			roadClone.Name = "DRAGGING"
			roadClone.AnchorPoint = Vector2.new(0.5, 0.5) -- NEW
			task.spawn(function()
				while true do
					if not roadClone or roadClone.Name ~= "DRAGGING" then break end
					local mousePos = UserInputService:GetMouseLocation()
					local stateImage = btn.Parent.Parent
					local guiPos = stateImage.AbsolutePosition
					local cityPos = btn.CityName.AbsolutePosition
					local relX = cityPos.X - guiPos.X
					local relY = cityPos.Y - guiPos.Y
					local dist = (Vector2.new(mousePos.X, mousePos.Y) - Vector2.new(cityPos.X, cityPos.Y)).Magnitude
					roadClone.Position = UDim2.new(0.039, relX, -0.04, relY)
					roadClone.Size = UDim2.new(0.058, 0, 0, dist) -- NEW
					roadClone.Rotation = math.deg(math.atan2(mousePos.Y - cityPos.Y, mousePos.X - cityPos.X)) + 90
					task.wait()
				end
			end)
		end)

It has been a couple of days and still found no solution how to fix this. Here is a video of what is happening right now:


The position is fixed all I just need is the size to go in one direction instead of both.