How do i get "Mouse.Z"

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!
    To get the mouse position for z
-- Client
Tool = script.Parent

UserInputService = game:GetService("UserInputService")
ClientRemote = Tool:WaitForChild("ClientRemote")

Mouse = UserInputService:GetMouseLocation()

UserInputService.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		local function GetMouseLocations()
			Mouse = UserInputService:GetMouseLocation()
			return Mouse.X, Mouse.Y
		end 
		
		ClientRemote:InvokeServer(GetMouseLocations())
	end
end)

--Server
Tool = script.Parent

ClientRemote = Instance.new("RemoteFunction")
ClientRemote.Name = "ClientRemote"
ClientRemote.Parent = Tool

ClientRemote.OnServerInvoke = function(Player, MouseX, MouseY)
	local Part = Instance.new("Part")
	Part.Parent = workspace
	Part.Position = Vector3.new(MouseX, MouseY, nil)
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Im sure u can simply get it from hit position of mouse

local MouseZ = mouse.Hit.Position.Z
1 Like

isnt that kind of pratice not advised
im also using the GetMouseLocation so i dont see how this would fix it

1 Like

GetMouseLocation returns the mouse’s position relative to the top-left corner of the screen. It does not return the World Space position of your cursor.

Although it’s not advised to use Mouse.Hit.Position, unless you write a custom replacement for this function, it’ll do the job for you.

oh, guess im gonna scrap getmouselocation and try raycastservice, thank you

1 Like

If you want to use a non-deprecated class, @ProgrammerNovice, you could try raycasting from the player’s Camera:

-- FETCH THE 2D POSITION OF THE PLAYER'S MOUSE ON THEIR SCREEN
local mouse: Vector2 = UIS:GetMouseLocation()

-- FETCH THE CAMERA FOR THE PLAYER
local camera: Camera = game.Workspace.CurrentCamera

-- ACCOUNTING FOR GUI INSET, RAYCAST FROM THE PLAYER'S CAMERA
-- TO A 3D POSITION IN THE WORKSPACE, AT A DEPTH OF 100
local ray: Ray = camera:ScreenPointToRay(mouse.X, mouse.Y, 100)

-- THE 3D POSITION EQUIVALENT TO mouse.Hit
print(ray.Origin)
1 Like

You dont have to show me a script for me to just copy and paste and then it work i was just on a video learning raycasting, im trying to learn without being shown or copy pasting someones work, thx tho

1 Like

That wasn’t what I was doing! When trying to teach others, I find it’s beneficial to show snippets of code in practice so people can see how things work. It’s then the individual’s decision as to whether they copy and paste things.

2 Likes

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