How can I get the mouse position in 3D space without using the Mouse object?

I want to get the 3d position so i can teleport the player to that after pressing button on there keyboard and this is what i got

local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRP = Character:WaitForChild("HumanoidRootPart")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = Player:GetMouse()
local camera = workspace.CurrentCamera
local viewportPoint = camera.ViewportSize / 2
local unitRay = camera:ScreenPointToRay(viewportPoint.X, viewportPoint.Y, 0)
local ray = Ray.new(unitRay.Origin, unitRay.Direction * 0)
local Target, position = workspace:FindPartOnRay(ray,Player,Character or nil)


print(ray)
wait(2)

if HumanoidRP then
HumanoidRP.Position = Vector3.new(ray)

If you can help in any way please help

1 Like

I’m not completely sure what you mean, but here’s the best I came up with:

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()

function KeyPressed() -- Use with ContextActionService
    character:MoveTo(mouse.Hit.p) -- Note you're moving the character; moving the humanoid will make it walk to the position
end
1 Like

Sorry i for got to tell u that this is Onasever event so i want the player to move at location of the mouse once its not a tool and i used an the the one that fire the server is this script
local Player = game:GetService(“Players”).LocalPlayer

local rep = game:GetService("ReplicatedStorage")
local World = rep:WaitForChild("TheWorldRemotes"):WaitForChild("TimeSkip")
local UIS = game:GetService("UserInputService")

local Enable = script.Parent:WaitForChild("Enable")
local Bragge = script.Parent:WaitForChild("Braggee")
local debounce = false
local cooldown = 10
 
UIS.InputBegan:Connect(function(input,IsTyping)
	if IsTyping then
		return
	elseif input.KeyCode == Enum.KeyCode.H  then
 
		if workspace:FindFirstChild(Player.Name.." Stand")and Enable.Value == false and Bragge.Value == false then
			if debounce == false then
				debounce = true

local camera = workspace.CurrentCamera 
local unitRay = camera:ScreenPointToRay(0,0,0)

				print(unitRay)
				World:FireServer(Player)

			end
		end
 
	end
end)
 


World.OnClientEvent:Connect(function()
wait(cooldown)
debounce = false
end)

Then this is the server script

local rep = game:GetService("ReplicatedStorage")
local World = rp:WaitForChild("TheWorldRemotes"):WaitForChild("TimeSkip")
local TweenService = game:GetService("TweenService")

local Debris = game:GetService("Debris")

local range = 100
local duration = 3

World.OnServerEvent:Connect(function(Player)
local Folder = Instance.new("Folder",workspace)
Folder.Name = Player.Name.." TimeSkip"

print("started")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRP = Character:WaitForChild("HumanoidRootPart")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = Player:GetMouse()
local camera = workspace.CurrentCamera
local viewportPoint = camera.ViewportSize / 2
local unitRay = camera:ScreenPointToRay(viewportPoint.X, viewportPoint.Y, 0)
local ray = Ray.new(unitRay.Origin, unitRay.Direction * 0)
local Target, position = workspace:FindPartOnRay(ray,Player,Character or nil)



print(ray)
wait(2)

if HumanoidRP then
HumanoidRP.Position = Vector3.new(ray)
end
wait(1)

local myMove = workspace:WaitForChild(player.Name.." TimeSkip")
myMove:Destory()
World:FireClient(Player)

end)
2 Likes

You are using Players.LocalPlayer, GetMouse() and workspace.CurrentCamera on the server ? you cant.
What are you trying to do exactly ?

Im trying to tp the person who pressed the button, where ever the mouse is on the map grounds

Ok, first, this is very dangerous because there is (almost) no way to verify if the 3D position of the mouse is legit. So you have to set a maxdistance.

You want to calculate the target position with GetMouse() on client, then send this position to the server, calculate if the position is valid, and then move the player. You dont need Raycasting for that

So i use the GetMouse().hit.p and get the 3d position and then send it to the server side and move the character, Right?

This might help you out.

Didnt know this existed, thanks!

@austingamer12345 you therefore want to do MouseModule:GetHit() (returns a Vector3), send it to the server side, do some verification with a MaxDistance / using the humanoidRootPart’s lookVector, and then move the character

1 Like

What I prefer to do is send the mouse direction to the server and let the server raycast, this also allows to add random spread in shooters to prevent exploiters. But it depends on what you’re trying to achieve.

2 Likes

He’s trying to teleport himself so i guess it doesnt really matter

No the player who has this ability and use this and thx

1 Like