Scripting a wire task like Among Us

Currently I have this GUI as a base,


(not the most beautiful GUI I know)

and I want to be able to connect these wires like how it is in Among Us. Problem is:

https://streamable.com/pe29mz

This happens. I know the problem here, I need to map this scale between each sides of my inside frame but I couldn’t think a way of doing that. Here is the related part of my script:

runServiceEvent = runService.RenderStepped:Connect(function()
		if holding then
			local frameCenter = copy.AbsolutePosition + (copy.AbsoluteSize/2)
			local x = math.atan2(mouse.Y - frameCenter.Y, mouse.X - frameCenter.X)
			copy.Rotation = math.deg(x)
			copy.Size = UDim2.new((mouse.X/mouse.ViewSizeX)/mainFrame.InsideFrame.Size.X.Scale + originalSize, 0, copy.Size.Y.Scale, 0)
		end
	end)
1 Like

The Video isn’t working, I don’t get ur mistake sorry.

1 Like

As stated above me, your video doesn’t work, so it’s hard to tell what exactly doesn’t work.

I recommend changing two things though, which may fix your issue: Calculate the wire size with Offset, not Scale, and calculate the middle position every frame, not the starting position.

--When creating your frame, make sure you set AnchorPoint and remember the start
local w = mainFrame.InsideFrame
copy.AnchorPoint = Vector2.new(.5, .5)
local start = UDim2.new(0, x, 0, y-36) - UDim2.new(0, w.AbsolutePosition.X, 0, w.AbsolutePosition.Y)
copy.Position = start


runServiceEvent = runService.RenderStepped:Connect(function()
    if holding then
    	copy.Position = start:Lerp(UDim2.new(0, m.X, 0, m.Y) - UDim2.new(0, w.AbsolutePosition.X, 0, w.AbsolutePosition.Y), .5)
	    local d = copy.Position - start
	    copy.Size = UDim2.new(0, math.sqrt(d.X.Offset^2 + d.Y.Offset^2) * 2, copy.Size.Y.Scale, 0)
    	copy.Rotation = math.deg(math.atan2(d.Y.Offset, d.X.Offset))
    end
end)

You could just calculate the Magnitude between both Positions ^^

What is this start value supposed to be? By the way, I actually set an anchor point but it isn’t in script.
DevForum seems broken because it didn’t embed my Streamable video either I’m not sure about the problem here. Video is there now.

1 Like

The start value is written to when the user first starts dragging a wire. With my algorithm it will define where the wire “starts”. You can either remember the mouse position (which is what x and y are in my example) or the middle absolute position of the wire segment the user started dragging.

2 Likes