Mouse.Target Error

So im trying to the players mouse target but it keeps erroring.

local script

local Module = require(game.ReplicatedStorage:WaitForChild("PainModule"))

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
Module.RemoveMoveMent(Player)

Mouse.Button1Down:Connect(function()
	script.EVENT:FireServer(Player)
end)

Server

script.Parent.EVENT.OnServerEvent:Connect(function(Player)
	local Mouse = Player:GetMouse()
	local MouseTarget = Mouse.Target
	print(Player.Name)
	print(Mouse.Target)
end)

error
12:29:29.614 Players.Tunnells.PlayerGui.PainClient.PainServer:3: attempt to index nil with ‘Target’ - Server - PainServer:3

Send your Mouse Target through the event, the mouse object cannot be used on the server. Remote events also automatically pass the player, so you dont need it in FireServer

This only happens when you aim your mouse to the air/sky.

EDIT: I read the script wrong, my bad

local Module = require(game.ReplicatedStorage:WaitForChild("PainModule"))

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
Module.RemoveMoveMent(Player)

Mouse.Button1Down:Connect(function()
	local MouseTarget = Mouse.Target.Position
	script.EVENT:FireServer(Player, MouseTarget)
end)
script.Parent.EVENT.OnServerEvent:Connect(function(Player, MouseTarget)
	print(MouseTarget)
end)

Now all it prints is my username

You dont need to pass the player in FireServer.

still prints my username :confused:

it’s because you fired the RemoteEvent like this

:FireServer(Player, MouseTarget)

you need to remove the “Player” variable, there’s no point of sending it (DO NOT remove the “Player” tuple in the server script, or else the “MouseTarget” tuple will be the player instead of Mouse.Target)

:FireServer(MouseTarget)

should work

local Module 
while not pcall(function()
		Module = require(game.ReplicatedStorage:WaitForChild("PainModule"))
	end) do
	wait()
end

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
Module.RemoveMoveMent(Player)

Mouse.Button1Down:Connect(function()
	if Mouse.Target then
		script.EVENT:FireServer(Player, Mouse.Target)
	end
	
end)

I fixed a little

--you need check mouse target not nil then fire it!
if Mouse.Target then
	script.EVENT:FireServer(Player, Mouse.Target)
end

First thing first, you do not need to fire player to the server. The 1st argument of OnServerEvent is player, which the second argument (Player) is not needed. → RemoteEvent | Roblox Creator Documentation (look at parameters)

Second, you can’t get the player mouse in ServerScript. Mouse (or inputs) is only accessible from LocalScript. → Mouse | Roblox Creator Documentation

Now, you must give the server an argument “mouseTarget”, and make mouseTarget to player’s mouse target in LocalScript.

game:GetService("ReplicatedStorage").RemoteEvent:FireServer(game:GetService("Players").LocalPlayer:GetMouse().Target)

Lastly, it’s optional to detect if the Mouse.Target is nil or not. You can print nil directly using

print(nil)

Heres the fixed LocalScript:

local Module = require(game.ReplicatedStorage:WaitForChild("PainModule"))

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
Module.RemoveMoveMent(Player)

Mouse.Button1Down:Connect(function()
    script.EVENT:FireServer(Mouse.Target)
end)

Fixed server script:

script.Parent.EVENT.OnServerEvent:Connect(function(Player, mouseTarget)
    print(mouseTarget)
end)

I hope it works… Thanks for reading

You have to call all mouse related objects inside a local script. Here :

Local Script

local Module = require(game.ReplicatedStorage:WaitForChild("PainModule"))

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
Module.RemoveMoveMent(Player)

Mouse.Button1Down:Connect(function()
	script.EVENT:FireServer(Player, Mouse.Target)
end)

Server Script

script.Parent.EVENT.OnServerEvent:Connect(function(Player, player, mouseTarget)
	local MouseTarget = mouseTarget
  	print(Player.Name)
	print(Mouse.Target)
end)