AttachmentA position not matching mouse.Hit.p

I finished making a script that made a rope constraint attached to wherever the player looked. I was proud of it, but as I worked on more advanced things, I realized that it would look better animated rather than a random rope constraint coming out of thin air. So what my idea was, was to make the Attachment0 and Attachment1 the same position. Then I would tween it to mouse.Hit.p to get a look that makes it look like it’s coming out of the gun. The problem is that it just randomly goes to a strange position when I press right click.

Here’s a video: https://gyazo.com/736185169723980d752226db79ab73d4

(I apologize for the viewmodel in the way, I forgot to set the transparency)

I’ll also post some code. This is AROUND the same code that I had before. I don’t have the old version so I’m going off what I remember.

Tool.Equipped:Connect(function()

	uis.InputBegan:connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			attachmentA.Position = mouse.Hit.p			
			ropeConstraint.Length = ((attachmentA.Position - Handle.Position).Magnitude) + 3
			if ropeConstraint.Length < 125 then
				ropeConstraint.Enabled = true
			end
		end
	end)

This is the script that I’m currently using right now.

local tweenInfoProj = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out, 0, false, 0)
local ProjTProperties = {Position = mouse.Hit.p}
local TweenProj = TweenService:Create(attachmentA,tweenInfoProj,ProjTProperties)
--Input Detection
Tool.Equipped:Connect(function()

	uis.InputBegan:connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			attachmentA.Position = Handle.CFrame.Position			
			ropeConstraint.Length = ((mouse.Hit.p - Handle.Position).Magnitude) + 3
			if ropeConstraint.Length < 125 then
				ropeConstraint.Enabled = true
				TweenProj:Play()
			end
		end
	end)

So I tried to see if there was anything in this peculiar position but I found nothing. Help would be appreciated.

2 Likes

Have you tried putting prints in each section of the script where positions are effected to see what is happening?
I have found it helpful when debugging with prints to include the name of the item with its value.
For example print(“attachmentA.Position=”,attachmentA.Position)

I had this problem, doAttachmentA.WorldPosition = Handle.CFrame.Position, also attachmentA.WorldPosition = mouse.Hit.p

1 Like

Thanks for the idea. Unfortunately the print and the output show nothing out of the ordinary. It’s just that the attachmentA position stays at one spot. I printed mouse.Hit.p and it actually printed to where my mouse was. Still not sure why its not working.

Sorry, but this didn’t work. Fortunately I found the solution and I will be commenting it shortly.

I went back through my code to find the source of the problem. It was because mouse.Hit.p is only defined once. Essentially, it was detecting where my mouse was as soon as the game started and always tweened there. So this is what I did. I made it so the variable “ProjTProperties” was told where the mouse was everytime it was clicked.

TLDR: I just switched some positioning in my code so that when right-click is pressed, it changes the tween properties. Here’s some code in case you don’t understand what I’m saying.

local tweenInfoProj = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out, 0, false, 0)
--Input Detection
Tool.Equipped:Connect(function()

	uis.InputBegan:connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			attachmentA.Position = Handle.CFrame.Position			
			ropeConstraint.Length = ((mouse.Hit.p - Handle.Position).Magnitude) + 3
			if ropeConstraint.Length < 125 then
				local ProjTProperties = {Position = mouse.Hit.p} --Change the tween value every right click.
				local TweenProj = TweenService:Create(attachmentA,tweenInfoProj,ProjTProperties)
				ropeConstraint.Enabled = true
				TweenProj:Play()
			end
		end
	end)

https://gyazo.com/0efd48c7264d51baa078245d7e932a72

1 Like