Problem with Mouse.Target

Code creates attachment to wrong possition. And when the player clicks to an anchored part, Rope.WinchSpeed is not working

local Players = game:GetService("Players")
local Mouse = Players.LocalPlayer:GetMouse()

local function onButton1Down()
	local character = Players.LocalPlayer.Character
	if not character then
		return
	end

	local leftHand = character:FindFirstChild("LeftHand")
	if not leftHand then
		return
	end

	local target = Mouse.Target
	if not target then
		return
	end
	local ip = leftHand:FindFirstChild("RopeConstraint")
	if not ip then
		local Rope = Instance.new("RopeConstraint")
		Rope.Parent = leftHand
		Rope.Length = (leftHand.Position - target.Position).Magnitude
		Rope.WinchEnabled = true
		Rope.WinchSpeed = 100
		Rope.Visible = true

		local At1 = Instance.new("Attachment")
		At1.Position  = Mouse.Target.Position
		At1.Parent = target
		At1.Name = "Attachment0"
		Rope.Attachment0 = At1

		local At2 = Instance.new("Attachment")
		At2.Parent = leftHand
		At2.Name = "Attachment1"
		Rope.Attachment1 = At2
	else
		ip:Destroy()
	end
end

Mouse.Button1Down:Connect(onButton1Down)

Maybe try to change this line of code
Old:

local character = Players.LocalPlayer.Character

New

local character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()
1 Like

Set The WorldPosition of the Attachment to the Position. Position on the Attachment is based on off the Object space of its parent, or simply its Local Axis

2 Likes

Don’t set the position of At1. The attachment’s WorldPosition is already set to the same position as the part you parent it to. By setting Position, you’re offsetting it. Remove this line:

At1.Position = Mouse.Target.Position

1 Like

Give me a sec I will fix it for you

1 Like

Oh yeah it’s work like that but I prefer raycast more than mouse target

1 Like

Fixed and improved some defines in the script

local Players = game:GetService("Players")
local Mouse = Players.LocalPlayer:GetMouse()

local function onButton1Down()
	local character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait() 
	if not character then
		return
	end

	local leftHand = character:FindFirstChild("LeftHand")
	if not leftHand then
		return
	end

	local target = Mouse.Target
	if not target then
		return
	end
	local ip = leftHand:FindFirstChild("RopeConstraint")
	if not ip then
		local Rope = Instance.new("RopeConstraint")
		Rope.Parent = leftHand
		Rope.Length = (leftHand.Position - target.Position).Magnitude
		Rope.WinchEnabled = true
		Rope.WinchSpeed = 100
		Rope.Visible = true

		local At1 = Instance.new("Attachment")
		At1.Parent = target
		At1.Name = "Attachment0"
		Rope.Attachment0 = At1

		local At2 = Instance.new("Attachment")
		At2.Parent = leftHand
		At2.Name = "Attachment1"
		Rope.Attachment1 = At2
	else
		ip:Destroy() 
	end
end

Mouse.Button1Down:Connect(onButton1Down)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.