How to get mouse.Hit.Position with a keyboard?

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!
    So I want to make it so that a keyboard press will fire a remote event with the mouse.Hit.Position.

  2. What is the issue? Include screenshots/videos if possible!
    I can’t really think of how to make this work

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I haven’t seen any posts on YouTube, Reddit, or the Dev forum addressing this issue.

local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent")

UIS.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	
	if input.KeyCode == Enum.KeyCode.Q then
		firing = true
                RE:FireServer(mouse.Hit.Position
        end
end
1 Like

Does the script give you errors or does it not work at all?

are you trying to send the position to the server when q is pressed? that’s really easy, but I see some syntax errors here:

RE:FireServer(mouse.Hit.Position)--add a closing parenthesis
--and in the last end
end)--also add a closing paren

also using contextAction service is easier, because you don’t need to check for the right keycode anymore:

local contextActionS = game:GetService("ContextActionService")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent")

contextActionS:BindAction("MouseSendAction", function(name, state) --in the func, you can
--also have the action name, state, and input object passed
--maybe you want to check if the input began?
if state ~= Enum.UserInputState.Begin then return end
  firing = true--even though firing isn't defined in your snippet,
--it probably is important. Delete this if it isn't
  RE:FireServer(mouse.Hit.Position)
end, false, Enum.KeyCode.Q)

you can then on the server handle the position:

local re = game.ReplicatedStorage.RemoteEvent--name your remotes please
re.OnServerEvent:Connect(function(plr, pos: Vector3)
print(plr.Name.."'s mouse is at: "..pos)
end)

Hope this helps!

2 Likes

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