Why is my mouse positioning not accurate

Hello, So i was making a script that would allow me to shoot at people after I press F. It’s been going great but the raycast is extremly off with the mouse. This is the line that calculates the mouse pos. This is a local script.

game.ReplicatedStorage.RemoteEvents.ShootFromPlayerEvent:FireServer(Mouse.Hit.LookVector, KillModuleEnabled.Value)

This is the line of the script that does the ray cast. This is a server script.

	local Shoot = workspace:Raycast(HRP.CFrame.Position, MouseVector * 999, ShootParams)

If you’d like the full script or have any questions then say below.

Can you show a video of whats going on?

https://gyazo.com/14112dfb39f92c64f5e55cf6abef86c4
look here

Could you show me the full script? What you sent above is supposed to be right

Which one, local are server script?

Local script

This is the local script:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local KillModuleEnabled = Player:WaitForChild("GameStats"):WaitForChild("KillModuleEnabled")
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(Input, processed)
	if Input.KeyCode == Enum.KeyCode.F then
		KillModuleEnabled.Value = not KillModuleEnabled.Value
		if KillModuleEnabled.Value == true and Player.UserId == 182343133 then
			Mouse.Button1Down:Connect(function()
				game.ReplicatedStorage.RemoteEvents.ShootFromPlayerEvent:FireServer(Mouse.Hit.LookVector, KillModuleEnabled.Value)
			end)		
		end
	end
end)

oh wait wait wait why are you passing a lookvector as your origin point?

Im passing the HumanoidRootParts Cframe.Position as the origin.

Hi!
I believe what you are looking for is mouse.Hit as a CFrame itself. Since mouse is a legacy object, and you are raycasting “on demand”, I suggest you use the following approach instead.

  1. Find mouse position on the screen (in pixels) using UserInputService:GetMouseLocation().
  2. Use gathered coordinates in camera:ScreenPointToRay() function, which provides a UnitRay as a return.
  3. Extend the UnitRay and check for results.

Example standalone code:

local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")

local RAY_LENGTH = 800

local player = game:GetService("Players").LocalPlayer
local camera = workspace.CurrentCamera

local function Shoot(actionName,inputState,inputObject)
	if inputState ~= Enum.UserInputState.Begin then return end
	
	local mousePos = UIS:GetMouseLocation()
	local unitRay = camera:ScreenPointToRay(mousePos.X,mousePos.Y)
	
	local raycastResult = workspace:Raycast(unitRay.Origin,unitRay.Direction*RAY_LENGTH)
	
	if raycastResult then
		local part = Instance.new("Part")
		part.BrickColor = BrickColor.new("Really red")
		part.Anchored = true; part.CanCollide = false
		part.Position = raycastResult.Position
		part.Parent = workspace
	end
end
CAS:BindAction("Shoot",Shoot,false,Enum.KeyCode.F)
  • note: ray origin may also be some other position, such as Head or HumanoidRootPart.

EDIT. @kinglol123468 I wouldn’t have sent the script if it didn’t work. :wink:
It is bound to F key.

Although I suggest the approach above, I’m also pasting the version involving mouse object. Mouse already provides you with necessary information, so raycasting isn’t necessary. It has mouse.Hit, mouse.Target, mouse.TargetFilter and other properties.

local CAS = game:GetService("ContextActionService")

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

local function Shoot(actionName,inputState,inputObject)
	if inputState ~= Enum.UserInputState.Begin then return end
	
	print(mouse.Target)
	
	if mouse.Target then
		local part = Instance.new("Part")
		part.BrickColor = BrickColor.new("Really red")
		part.Anchored = true; part.CanCollide = false
		part.Position = mouse.Hit.Position
		part.Parent = workspace
	end
end
CAS:BindAction("Shoot",Shoot,false,Enum.KeyCode.F)
1 Like

Your script didn’t work.

This text will be blurred

Ok it works when I press F. So thank you for that. I’ll try to incorporate this with my shooting system. But one more questions. How can I make it detect if the player clicks?

No problem!
In case you would like to listen for specific mouse keys, you would compare UserInputType instead of KeyCode. The following should work. Good luck!

UserInputService:

UIS.InputBegan:Connect(function(input,gameProcessed)
    if gameProcessed then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        print("Clicked")
    end
end)

ContextActionService:

local function LeftMouseClick(actionName,inputState,inputObject)
    if inputState == Enum.UserInputState.Begin then
        print("Clicked")
    end
end

CAS:BindAction("LeftMouseClick",LeftMouseClick,false,Enum.UserInputType.MouseButton1)
1 Like
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	local mousePos = mouse.Hit --cframe value of current mouse position in window
	local mousePos2 = mouse.Hit.p --vector3 value of current mouse position in window
end)

Getting the mouse position from the “mouse” instance after a left mouse button click is relatively easy, you just need to make use of the “Button1Down” event which fires whenever the left mouse button is clicked.

just a note, its not good to check on the client, since hackers can just easily bypass it. Instead, check it on the server where you listen for the game.ReplicatedStorage.RemoteEvents.ShootFromPlayerEvent

This direction vector is relative to the camera and NOT the character.

Here you are shooting it relative to the character.

Instead you need the direction from the character which can be done simple by negating

local Direction = (Mouse.Hit.Position - Character.HumanoidRootPart.Position).Unit -- Subustitute for Mouse.Hit.LookVector

Small note: make sure you turn the vector into a unit on the server or the player might be able to exploit it and have infinite range.#

Another small note: Mouse.Hit can be nil. check for that too.