How to find mouse z?

i’m currently trying to make a build system inside of studio for my game (small island game) and i found the x and y of the mouse-where the part should go- but can’t find the z. I understand that there’s no z property inside of mouse, but I can’t find any other way to do it. i’ve tried

part.CFrame = CFrame.new(mouse.X, mouse.Hit.p.Y + 0.1, mouse.Hit.p.Z)

and

part.CFrame = CFrame.new(mouse.X, mouse.Hit.p.Y + 0.1, mouse.Hit.CFrame.Z)

, but nothing seems to work.

here’s my whole script

local plr = game.Players.LocalPlayer
local PosZ
local mouse = plr:GetMouse()
local uis = game:GetService("UserInputService")
local db = false
local mainpart = game.ReplicatedStorage.buildobjects.floor
local en = false

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q and db == false then
		db = true
		en = not en
		
		if en == true then
			mouse.Button1Down:Connect(function()
				local part = mainpart:Clone()
				
				part.Parent = workspace
				part.CFrame = CFrame.new(mouse.X, mouse.Hit.p.Y + 0.1, mouse.Hit.p.Z)
			end)
		end
		
		task.wait(0.1)
		db = false
	end
end)

please help

1 Like

I am pretty sure that you have to fire a raycast from the mouse to find the position. I am not very familiar with raycasts though, so hopefully the documentation or tutorial will help you.

2 Likes

Mouse.Hit is already a CFrame, there is a Z property in the Hit property, but not in the Mouse position itself, also Mouse has been superseded by UserInputService and ContextActionService

Im not sure why did you put the mouse X instead of the hit

2 Likes

You should do something like this:

local mouse = script.Parent.Parent:GetMouse()
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input,gpe)
	if gpe then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print(mouse.Hit.p) --this is the mouse's position in workspace
	end
end)

Though you can use mouse.Button1Down, but whatsoever, UIS might be better.