Mouse.Hit.Position or Mouse.Hit.p Errors [Solved]

Hello! Im having issues with Mouse.Hit.Position

The error is , attempt to index nil with ‘Position’ " i tried many solutions and none worked. (DevHub, Devforum)

If you have any idea how to fix this, please tell me.

2 Likes

May I see your script?

Your local script.

There are currently 2 things I believe may have happened.

  1. You are referencing mouse from a tool.Equipped connection
  2. You are trying to get the mouse and didn’t capitalize correctly.

Sure! But i wont send all of the script. (300+ Lines)

HoldGripClone.Position = Position + ((Hit.p - Position).Unit * DISTANCE)

Show the script where you stored your mouse

Can we see where you create the “Hit” variable? This line doesn’t give much for us to go off of.

You still have to define what objects position you want unless its stored as a variable, so it would look more like this:

HoldGripClone.Position = HoldGripClone.Position + ((Hit.p - Position).Unit * DISTANCE)

Ill remove alot of things that dont do anything with this.

--Get player
Player = game.Players.LocalPlayer

--Get mouse
Mouse = Player:GetMouse()

--On Button1Down
Mouse.Button1Down:connect(function()

	--Get
	local Character = Player.Character

	--Check DB
	if DB == false then

		--Set DB to true
		DB = true

		--Get Target and hit
		Target = Mouse.Target
		Hit = Mouse.Hit

		--Check if character exists
		if Character.PrimaryPart then

			--Check
			if Target and Target.Name ~= "Workspace" and not(Target.Anchored) and not Target.Parent:FindFirstChild("Humanoid") and (Target.Position - Character.PrimaryPart.Position).Magnitude <= GRABDISTANCE and Target.Transparency ~= 1 then

				--Get holdgrip
				HoldGripClone = HoldGripScript:Clone()

				--Get pos
				local Position = Character.PrimaryPart.Position

				--If Position exists
				if Position then

					--Set
					HoldGripClone.Position = Position + ((Hit.p - Position).Unit * DISTANCE)
					local X, Y, Z = CFrame.new(Character.PrimaryPart.Position, Target.Position):ToOrientation() HoldGripClone.Orientation = Vector3.new(0, math.deg(Y), 0) + Rotation

				end

				--Set parent
				HoldGripClone.Parent = workspace

				--Attachment for Target
				local Attachment0 = Attachment:Clone()

				--Set
				if Target then Attachment0.CFrame = Target.CFrame:ToObjectSpace(Hit) end
				Attachment0.Name = "DragAttachment"
				Attachment0.Parent = Target

				--Set AlignPosition
				local gripAlignPosition = HoldGripClone.GripAlignPosition
				gripAlignPosition.Attachment0 = Attachment0

				--Set AlignOrientation
				local gripAlignOrientation = HoldGripClone.GripAlignOrientation
				gripAlignOrientation.Attachment0 = Attachment0

			else --Else statement is false

				--Reset
				reset()

			end

		end

	end

end)

He probably stored the position variable somewhere.

Where’s your mouse varible?
image

1 Like

Oh yeah forgot that, but its in there. (Fixed the showcase code)
image

1 Like

I think its because the Mouse.Hit is a CFrame value and .Position is a Vector3. So instead try this:

local Position = Character.PrimaryPart.CFrame

Setting .Position after CFrame gets you CFrame position.

CFrame.Position --Return CFrame position.

image
Try changing it with Mouse.Hit.p

1 Like

Tried both Mouse.p and Mouse.Position .

The Mouse.p and Mouse.Position Doesn’t exists.
try Mouse.hit.p and Mouse.hit.Position

Sorry i did typo. i meant the Hit.Position and Hit.p

Well I’ve tried making it
you can try this

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()


Mouse.Button1Down:Connect(function()
	local Target = Mouse.Target
	local Hit = Mouse.Hit
	
	print(Hit.Position)
end)

Well i can try printing the position, also it errors only sometimes.

Maybe try using a waitforchild()? Maybe the holdgrip or mouse isnt loading fast enough for the code, if thats not right then honestly I have no idea.