im trying to create a wire task im gonna add custom physics later to make it kinda look like the ones from bite by night but i’ve being trying to fix an offset in the mouse’s position for a while
local uis = game:GetService(“UserInputService”)
local run_service = game:GetService(“RunService”)
local origin = script.Parent.AbsolutePosition
local mouse = game.Players.LocalPlayer:GetMouse()
run_service.RenderStepped:Connect(function(dt)
local change =uis:GetMouseLocation()-origin
script.Parent.Rotation = math.deg(math.atan2(change.Y, change.X))
script.Parent.Position = UDim2.fromOffset(mouse.X/2, mouse.Y/2)+UDim2.fromOffset(origin)
script.Parent.Size = UDim2.fromOffset(change.Magnitude, 100)
end)
actually better version this one just accepts whatever root (so have localscript in frame or gui or wtv doesn’t really matter)
first click is setting origin, second click sets end. it just does this on loop obv replace what you need this is just me setting a demo.
--!strict
-- Services
local GuiService = game:GetService("GuiService")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
-- Configuration
local WIRE_COLOR = Color3.fromRGB(255, 255, 255)
local WIRE_THICKNESS = 4
-- References
local screenGui = script:FindFirstAncestorOfClass("ScreenGui") :: ScreenGui?
assert(screenGui, "LocalScript must be inside a ScreenGui.")
-- State
local startPosition: Vector2? = nil
local activeWire: Path2D? = nil
-- Convert the cursor position into the ScreenGui's coordinate space.
local function getMousePosition(): Vector2
local inset = GuiService:GetGuiInset()
return UserInputService:GetMouseLocation()
- inset
- screenGui.AbsolutePosition
end
-- Convert a pixel position into a Path2D control point.
local function createPoint(position: Vector2): Path2DControlPoint
return Path2DControlPoint.new(
UDim2.fromOffset(position.X, position.Y)
)
end
-- Update both endpoints of a wire.
local function updateWire(wire: Path2D, from: Vector2, to: Vector2)
wire:SetControlPoints({
createPoint(from),
createPoint(to),
})
end
-- Preview the wire while selecting its second endpoint.
RunService.RenderStepped:Connect(function()
if activeWire and startPosition then
updateWire(activeWire, startPosition, getMousePosition())
end
end)
-- Alternate between selecting the start and end points.
UserInputService.InputBegan:Connect(function(input: InputObject)
if input.UserInputType ~= Enum.UserInputType.MouseButton1 then
return
end
local position = getMousePosition()
-- Second click: finalize the current wire.
if activeWire and startPosition then
updateWire(activeWire, startPosition, position)
activeWire = nil
startPosition = nil
return
end
-- First click: create a new wire.
local wire = Instance.new("Path2D")
wire.Name = "Wire"
wire.Color3 = WIRE_COLOR
wire.Thickness = WIRE_THICKNESS
wire.Parent = screenGui
startPosition = position
activeWire = wire
updateWire(wire, position, position)
end)
(again placeholder this isn’t optimized or wtv this is just to show how path2d works)