Issues with :GetMouse().Hit

for some reason, i cannot get .hit to work (even though ive used it many times before)

this is my localscript

local RemoteFunc = script.Parent.GetMouse

RemoteFunc.OnClientInvoke = function()
	local Mouse = game.Players.LocalPlayer:GetMouse()
	print(Mouse.Hit)
	return Mouse.Hit
end

and it returns an insane value in the middle of the void any time i call on it over a part

serverscript

local function GetMouseHit(Character)
	local Mouse = Tool.GetMouse:InvokeClient(game.Players:GetPlayerFromCharacter(Character)) :: CFrame
	print(Mouse)
	
	return Mouse.Position
end

all parented to a tool (including the remotefunc)

Ive had a very similar problem aswell, if i figure something out ill be sure to let you know

Does it print the insane value from both scripts or just the server?

Does this happen on every part?

both scripts and yes
roblox character limit is an unneeded feature

1 Like

It could be due to the remote function, you should try getting the hit position outside this and see if it functions as normal


Though, it should still work. Try this:

local function mouseHit()
    local mousePos = game.UserInputService:GetMouseLocation()
    local ray = workspace.CurrentCamera:ViewportPointToRay(mousePos.X, mousePos.Y)
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character or nil}

    local result = workspace:Raycast(ray.Origin, ray.Direction * 1000, raycastParams)
    if result then
        return CFrame.new(result.Position), result.Instance, result.Distance -- so you dont need more functions (can be used like this: local hit, hover, dist = mouseHit(), or like this: local hit = mouseHit() or even like this: local _, hover, _ = mouseHit()
    end

    return nil -- wont return nil cuz of another return (unless theres no result)
end

This seems to be an attempt to avoid hackers.
Correct me if I’m wrong here, but this is a tool on the client, and both scripts are accessible.
So you may as well do it the normal way.

With a RemoteEvent;

LocalScript

local RemoteEvent = script.Parent:WaitForChild("MouseHit")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

script.Parent.Equipped:Connect(function()
	mouse.Button1Down:Connect(function()
		RemoteEvent:FireServer(mouse.Hit)
	end)
end)

ServerScript

local RemoteEvent = script.Parent:WaitForChild("MouseHit")

RemoteEvent.OnServerEvent:Connect(function(plr, hit)
	print(hit.Position)
end)