How do I make a connecting line GUI?

Hi there, I am trying to make a Gui wire thing that will connect to a certain spot. And it can only connect to the right color / labe.l (e.g. green - color, and when you connect the green wire it transfers the color properties.) Can anyone help with that? I am having trouble with pointing the GUI in the direction of the mouse and being able to drag and extend it toward the mouse…

Save the starting point, then, as the mouse is being dragged, set the GUI anchor points to {0.5,0.5} and set its position to (StartPosition + CurrentMousePosition) / 2 as offset, of course. If you want it to snap to the start position and the end position when close enough, make an if check on the distance:

local SnapDistance = 10
local EndPosition = Vector2.new(30,30) -- Whatever the target position of the GUI is
local mouse = game.Players.LocalPlayer.GetMouse()
local CurrentMousePosition = Vector2.new(mouse.X, mouse.Y)

function GetDistance(start, target)
    return (target - start).Magnitude
end

If GetDistance(CurrentMousePosition, EndPosition) >= SnapDistance then
    CurrentMousePosition = EndPosition
end

Of course, this is all pseudocode, I expect you to be able to learn from this and apply it to your own code.

Good luck with your Among Us clone

3 Likes

Among us clone? (how did u know?) also I was trying to make the node things inside of blender, unity and other engines. (Because you can’t script in-game…)

Also @Orodex I think its supposed to be - player:GetMouse() not player.GetMouse() right?

Also for the wire rotation you’ll also have to do, after you’ve set the position:

local wireStart = Vector2.new(7,11) --absolute position in pixels
local mousePos = Vector2.new(mouse.X,mouse.Y)
local v = mousePos-wireStart
local angle = math.deg(math.atan2(v.Y,v.X))

you’d obviously just set the rotation property of the wire UI after setting its position to the “angle” variable.

1 Like

but wait it keeps giving me errors saying that expected udim2 but got vector 2… please help with that I don’t know how to convert Vector2 to Udim2

Actually the script doesn’t work for some reason…

For the easiest results Id probably save AbsolutePosition property of a GUI at the start and end positions (which is already a Vector2)

Can you please explain, I have never used absolute values in roblox…

Also It doesn’t snap to the end

Just like

local position = gui.AbsolutePosition

You cant set it but you can save it, and that should be all you need to do for both of the scripts

Also the magnitude from the mouseposition and end position is always zero…

Okay it works but the wire is not connected to the start point. it just weirdly follows the mouse on an offset and wiggles around… (it point correctly and sizes correctly tho…)

local mouse = game.Players.LocalPlayer:GetMouse()
local wireStart = script.Parent.AbsolutePosition --absolute position in pixels

while wait() do
	
	local mousePos = Vector2.new(mouse.X,mouse.Y)
	local v = mousePos-wireStart
	local angle = math.deg(math.atan2(v.Y,v.X))
	
	script.Parent.Rotation = angle
		
	script.Parent.Size = UDim2.new(0,(mousePos - wireStart).Magnitude,0,20)
	
end

sry I’m lagging

https://i.gyazo.com/3ad1ea90d4bb92ca11241594aa25ea09.mp4

Make sure you set the anchor point to 0.5,0.5 and set its position to:

local v2pos = (startpoint+endpoint)/2
ui.Position = UDim2.new(0,v2pos.X,0,v2pos.Y)

Sorry Im late