mouse.Hit CFrame value isn't correct

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am attempting to create a grapple hook system, and am currently trying to get a rope constraint to attach to whatever the mouse is pointing at.

  1. What is the issue? Include screenshots / videos if possible!

I have a system in place to create an attachment at where the mouse is pointed (using mouse.Hit/CFrame), and at the tip of the grapple shooter. The system then creates a rope constraint using both of said attachment points. For whatever odd reason, the second attachment point where the mouse lays is not correct.

Here is my code and a video of the issue:

LocalScript inside of StarterCharacterScripts

local UIS = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()

local LAttach = char.ODM.LeftGrapple.LGrapple3.LRopeAttachment
local RAttach = char.ODM.RightGrapple.RGrapple3.RRopeAttachment

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		local mousehit = mouse.Target
		local mousecf = mouse.Hit
		game.ReplicatedStorage.RightOdm:FireServer(mousehit, mousecf)
	end
end)

Server Script inside of ServerScriptService:

game.ReplicatedStorage.RightOdm.OnServerEvent:Connect(function(plr, hit, cf)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local mouse = plr:GetMouse()

	local LAttach = char.ODM.LeftGrapple.LGrapple3.LRopeAttachment
	local RAttach = char.ODM.RightGrapple.RGrapple3.RRopeAttachment
	
	local lefttarget = Instance.new("Attachment")
	lefttarget.Parent = hit
	lefttarget.CFrame = cf

	local leftrope = Instance.new("RopeConstraint")
	leftrope.Parent = char.ODM.LeftGrapple.LGrapple3
	leftrope.Color = BrickColor.new("Black")
	leftrope.Thickness = 0.05
	leftrope.Attachment0 = LAttach
	leftrope.Attachment1 = lefttarget
	leftrope.Length = math.abs((LAttach.WorldPosition - lefttarget.WorldPosition).Magnitude)
	leftrope.Visible = true
end)

(upload://yrEnWnD3XsBh81RGYziZz8t86rs.mp4)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I haven’t found any similar problems with relevant answers through my own searches (though I am aware I may have missed something).

I have tried switching between declaring mouse.Hit and mouse.Target as variables and inside of the actual code itself, tried in both client and server sided scripts, and have tried position and cframe.

Well then maybe it isn’ t a Mouse,Hit issue, can you show a video so I can see how it looks like with what you have right now?

Sorry, my video didn’t embed. Here is a link.

Have you tried setting both attachments or just one and another’ s cframe value to Hit?

Maybe Change this to lefttarget.CFrame = -cf? It seems like the attachment is in the opposite direction. Not too sure but try it out!

No, I tried setting mouse.Hit to mouse.Hit:Inverse() but that did not solve my issue. There is still an offset of the attachment point.

You mean switching the values around and/or setting both of their CFrame values to hit? I will try that.

is it possible if you can print the “mouse.hit” from the client and the “cf” from the server? wanna compare the values

wait, would there be any difference if you changed lefttarget.CFrame = cf to lefttarget.CFrame = CFrame.new(cf)? I think you may have set it to a position value instead of cframe

No, mouse.Hit returns a CFrame value, while mouse.Hit.p returns a positional value from the CFrame value. I get the following error if I try to do that:

ServerScriptService.ODMServer:10: invalid argument #1 to ‘new’ (Vector3 expected, got CFrame) - Server - ODMServer:10

Hold on, I may have found the issue. A pair of values is mismatching, I believe this is one of the orientation values? Does anyone know what this may be?

MIGHT OF FOUND SOMETHING:

lefttarget.CFrame = cf

Set it to WORLD Cframe

lefttarget.WorldCFrame = cf

Let me know if it works!

1 Like

Ay, that worked! Thanks dude. That had me stumped for awhile.

1 Like

No Problem! I was a bit confused at first too but I compared the cframe to the worldCframe value and realized that the world CFrame was the actual Cframe of the part! :laughing:

1 Like

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