When transferring mouse.target too server with remote events it is nil

Hello, Im trying to get the mouse.Target from the client using remote events, but when I do so, the product turns nil. How do I successfully get the mouse.Target from the client using remote events

Code:

Client:

local mouse = player:GetMouse()

mouse.Button1Down:Connect(function () 
	print(mouse.Target)
	if game.ReplicatedStorage.WhichAbility.Value == "Ability1" then
		game.ReplicatedStorage.Fire:FireServer(mouse.Target) 
		print(mouse.Target)
	elseif game.ReplicatedStorage.WhichAbility.Value == "Ability2" then
		game.ReplicatedStorage.Fire2:FireServer()
	end
end)

Server:

game.ReplicatedStorage.Fire2.OnServerEvent:Connect(function (player, target)
	print(target, player)
end)

I have not tested in studio, but mouse.Target could be nil in case when your mouse hovers on the skybox not aiming at any object. You should set a check in the clientscript to negate nil and avoid sending nil over to server through remotes unless you need that.

local mouse = player:GetMouse()

mouse.Button1Down:Connect(function () 
	if mouse.Target then
	if game.ReplicatedStorage.WhichAbility.Value == "Ability1" then
		game.ReplicatedStorage.Fire:FireServer(mouse.Target) 
		print(mouse.Target)
	elseif game.ReplicatedStorage.WhichAbility.Value == "Ability2" then
		game.ReplicatedStorage.Fire2:FireServer()
	end
end
end)

If mouse.Target is the issue and always returns nil, you can always try mouse.Hit and check if that exists.

I have a print statement that tells me what mouse.target is before its transferred to the server. I know for a fact its not nil before it enters the remote event

It seems to be working fine when I do a test in studio.

Client:

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

mouse.Button1Down:Connect(function() 
	print("Client: ", mouse.Target)
	game.ReplicatedStorage.Fire:FireServer(mouse.Target) 
end)

Server:

game.ReplicatedStorage.Fire.OnServerEvent:Connect(function (player, target)
print("Server: ",target)
end)

image

1 Like

Your Fire2 remote doesnt have any arguments in the client script. You are listening to the Fire2 event that passes nil as argument. You should add in arguments for that.

1 Like

I put the arguments in for the wrong remote event oops my bad. I meant to put it into the ability2 one lol

1 Like