Why is this nil?

local script

local mouse = game.Players.LocalPlayer:GetMouse()
	
	local hit = mouse.Target
	
	return hit

server script

local mouseHit = game.ReplicatedStorage.getMouseLocation:FireClient(plr)
		print(mouseHit)

it prints “nil”. why is this?

note: i have tried printing “hit” on the client side, and it worked

You appear to be firing a remote event. Remote events do not return any values, which is why it returns nil. Consider using a remote function instead

1 Like

Yes, when i print it on the client side it prints the right thing

you should do this instead:

local script:

game.ReplicatedStorage.getMouseLocation.OnClientEvent:Connect(function()
	local mouse = game.Players.LocalPlayer:GetMouse()

	game.ReplicatedStorage.Target:FireServer(mouse.Target)
end)

script:

game.ReplicatedStorage.getMouseLocation:FireClient(game.Players:FindFirstChildWhichIsA("Player"))

game.ReplicatedStorage.Target.OnServerEvent:Connect(function(Thing)
	print(Thing)
end)
game.ReplicatedStorage.getMouseLocation:FireClient(game.Players:FindFirstChildWhichIsA("Player"))

game.ReplicatedStorage.Target.OnServerEvent:Connect(function(Player,Thing)
	print(Thing)
end)

OnServerEvent / OnServerInvoke requires the first argument to be the player.

youll need to communicate to the client and returns back the client’s mouse position
you can use remote functions to do that

--[[-------------------------------------------------------------------------------------------
SERVER SCRIPT
---------------------------------------------------------------------------------------------]]


local RequestFromClient = game.ReplicatedStorage.RemoteFunction -- remote function

RequestFromClient.OnServerInvoke = function(Player, MouseX, MouseY )
	-- the request 
	print(Player.Name, "Mouse X position is", tostring(MouseX), "And Mouse Y position is", tostring(MouseY))
end

-- example request the client to get its mouse position
game.Players.PlayerAdded:Connect(function(Player)
	repeat
		pcall(function()
			RequestFromClient:InvokeClient(Player)
		end)
		task.wait(1.5)
	until
	Player == nil
end)

--[[-------------------------------------------------------------------------------------------
LOCAL SCRIPT
---------------------------------------------------------------------------------------------]]

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

local RequestFromClient = game.ReplicatedStorage:WaitForChild("RemoteFunction") -- remote function

RequestFromClient.OnClientInvoke = function()
	RequestFromClient:InvokeServer(Mouse.X, Mouse.Y)
end

he just need what object the mouse it hovering to, not the position of the mouse

He should instead use a remote function:

-—----
-- local script

game.ReplicatedStorage. getMouseLocation.OnClientInvoke = function(player)
    return player:GetMouse().Target
end
local mouseHit = game.ReplicatedStorage.getMouseLocation:InvokeClient(plr)
print(mouseHit)
1 Like

Ok thanks i didnt knew how remote functions work before

Basically very similar to remote events, however they can return things.

1 Like

right but it shows how server request on a client

i forgot to say ‘like client’s mouse position’ eh

"attempt to index nil with ‘getmouse’ "

may i add that im invoking client from the server? is that something im able to do? if not, i need a way to do that