Remote Event Problem

I need help with fixing this bug in my code.

Server Script:

local gun = script.Parent.Parent

local getMouseRemote = game.ReplicatedStorage.GetMousePosition

gun.Activated:Connect(function()
	local player = game.Players:FindFirstChild(gun.Parent.Name)
	local rayOrigin = gun.Handle.rayOrigin.Position
	
	local mousePosition = getMouseRemote:FireClient(player)
	
	local directionToFire = (mousePosition - rayOrigin).Unit * 100
	
	local raycastResult = workspace:Raycast(rayOrigin, directionToFire)
	
	if raycastResult then
		print(raycastResult.Instance)
		print(raycastResult.Position)
		print(raycastResult.Distance)
	
	end
	
	if raycastResult then
		local humanoid = raycastResult.Instance.Parent:FindFirstChild("Humanoid")
		local head = raycastResult.Instance:FindFirstChild("Head")
		if head then
		humanoid:TakeDamage(60)
		else
			humanoid:TakeDamge(25)
		end
	end

end)

Local Script (I put this inside the handle):

game.ReplicatedStorage.GetMousePosition.OnClientEvent:Connect(function()
	return game.Players.LocalPlayer:GetMouse().Hit.Position
end)

The error I’m getting:
Workspace.Tool.Handle.ShootScript:11: attempt to perform arithmetic (sub) on nil and Vector3

I think it’s a problem relating to the remote event? Then again, still learning scripting so I could be wrong about that.

Any amount of help is appreciated.
Cheers!

you are sending a remote to the client but the client never returns anything. in the local script the “return” only returns from the function. you need to call the same or another remote-event on the client and make the server listen for that event. It might seem confusing since when calling a function that exists inside the script your code would be completly correct. you just arent returning data from the client in the correct way

the error you’re getting just means that you’re trying to do nil - Vector3 on line 11. Looking at your code the nil value would be mousePosition. It’s because you’re using a remote event and expecting it to return a value, you should switch to using remote function as they can return values

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.