Why am I getting "invalid argument #2 (Vector3 expected, got nil)" with .magnitude?

Hi, I have a problem where I get the error as stated in the title when I am getting the position using player:GetMouse(), then using HumanoidRootPart.Position - mouse.Hit.Position.

here is the code:

local mouse = player:GetMouse()

userInput.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.R and gpe == false then
		if tool.Parent == player.Backpack or character then
			if debounce == true then

server script:

remoteEvent.OnServerEvent:Connect(function(player, mouseP)
	if (humanoidRootPart.Position - mouseP).magnitude <= 50 then
		distance.Value = .2

But the weird part is that it happens randomly it feels like.

anybody know?

just make sure your mouse.hit.position is actually not nil it maybe giving nil back and not a vector3 position at times

you could do

if mouseP and (humanoidRootPart.Position - mouseP).magnitude <= 50 then

If it is nil, then how would I fix this? it still gives me error as well

it maybe happening because the tool is unequip and you are still able to fire it try removing the tool.Parent == player.Backpack

you can even do something for debugging it like printing both your positions at the top of your serverevent


Ok, but another thing is I am calling the mouse from a renderstepped, then firing a remote event then setting the variable equal to mouseP.

like this:

	if debounce2 == true then
local mouseP

remoteEvent2.OnServerEvent:Connect(function(player2, mousePosition)
	mouseP = mousePosition

oh thats totally different from the script above
I am not sure I would fire it like this maybe even just use a mousechanged on userinput for when it changes position

Renderstepped is not something you should probably fire constantly to the server most cases you want to use it for something visual on the client side that needs updated

what is this script used for or what are you trying to achieve with it?

So it is kind of like this crystal thing from elemental battlegrounds, and I am updating the mouse position while the parts are flowing in workspace


At the very beginning of the event, the mouse position does print nil once, then it doesn’t anymore for the mouse position

ok so maybe you should only send over mouse update as the parts are being added to the workspace. from what i see the mouse pos is being used to aim the parts projection?

yes, so everytime the parent of a part changes, then it updates mouse position?

yeah thats because you have it on renderstep which is being fired before the mouse is setting hit

ohhhhhhh that makes sense, so the frames are firing too fast for the mouse

it could. what I would do which you maybe is I create and render all on the client just send the type of attack and then positions they need to render to to the server then it fires and gives that info to all clients to render

So more client sided than server sided? sorry i’m a little new

yeah on first update. sometimes heartbeat can be used also but you are firing so many times to the server with that render it will probably cause some lag or timeouts even if over firing the event

yeah I wanted to use heartbeat, but it is deprecated

lol seems some things get deprecated that are very useful and some have it listed when they are not
yeah its cool I learn everyday mainly from trial and error and there maybe even a better way to do it than what I am suggesting I have tested a few ways and find that you client render and just send server values to update other clients really with projectiles. and depending on your hit use raycasting detections

many things you can create and have only client side physics and nothing but a value server side