Weird error with the mousePos

  1. What do you want to achieve?
    I was making a gun, then this error popped up.

  2. What is the issue?
    In the output, on line 58 keeps printing this error "
    Players.MalAllie.Backpack.Sniper Rifle.Server:58: attempt to perform arithmetic (sub) on nil and Vector3
    "

  3. What solutions have you tried so far?
    I’ve tried printing to see which argument is nil. The mousePos variable is nil. I can’t figure out why??? On the client, it seems perfectly fine??? I used similiar code like this but this one errors???

--Server

RemoteShoot.OnServerEvent:Connect(function(plr, mousePos: Vector3)
	if debounce or not plr.Character then
		return
	end

	if Ammo.Value <= 0 then
		reload(plr)

		return
	end
	
	local character = plr.Character
	local head: BasePart = character:WaitForChild("Head")
	local myHRP: Part	= character:WaitForChild("HumanoidRootPart")
	
	local origin = head.Position
	
	params:AddToFilter(getAllCharactersInTeam(plr.Team))
	local cast = workspace:Raycast(origin, (mousePos - origin), params) --this is line 58!!
	
	if cast then
		local model: Model = cast.Instance:FindFirstAncestorOfClass("Model")

		if model then
			local humanoid = model:FindFirstChildOfClass("Humanoid")

			if humanoid then
				local enemyPlr = game.Players:GetPlayerFromCharacter(model)
				local isAnEnemy = false

				if enemyPlr then
					if enemyPlr.Team ~= plr.Team then
						isAnEnemy = true

						local eProp: Model = model:WaitForChild("Properties")
						local playerDamagedMe: ObjectValue = eProp:WaitForChild("PlayerDamagedMe")
						playerDamagedMe.Value = plr
					end
				end

				if not enemyPlr then
					isAnEnemy = true
				end

				if isAnEnemy then
					local dmg = 45

					if cast.Instance.Name == "Head" then
						dmg = 45*3
					end

					humanoid:TakeDamage(dmg)
					KBConstructor.new(model:WaitForChild("HumanoidRootPart"), myHRP.CFrame.LookVector.Unit*dmg/3, 25)
					DamageDisplay:FireClient(plr, cast.Position, dmg ~= 6 and true or false, dmg)
				end
			end
		end
	end
	
	debounce = true 
	
	task.wait(.1)
	debounce = false
end)

--Client

Tool.Activated:Connect(function()
	local mousePos = mouse.Hit.p
	
	if mousePos == nil then
		print("mousePos == nil") return
	end
	
	RemoteShoot:FireServer(mousePos)
end)

Based off this alone I can’t conclude to much; may I see the local script? More specifically, where you declare mousePos.

local UserInputService									= game:GetService("UserInputService")
local ContextActionService								= game:GetService("ContextActionService")
local RunService 										= game:GetService("RunService")

local Tool												= script.Parent
local Sniper 											= Tool:WaitForChild("Sniper")
local RemoteShoot										= Tool:WaitForChild("RemoteShoot")

local plr												= game.Players.LocalPlayer
local camera 											= workspace.CurrentCamera
local mouse												= plr:GetMouse()

local GuiScope: ScreenGui								= plr.PlayerGui:WaitForChild("Scope")
GuiScope.Enabled = false

local equipped											= false
local isScoped											= false
local currentLerp										= nil

Sniper.Transparency 									= 1

local function reload()
	RemoteShoot:FireServer()
end

Tool.Activated:Connect(function()
	local mousePos = mouse.Hit.p
	
	if mousePos == nil then
		print("mousePos == nil") return
	end
	
	RemoteShoot:FireServer(mousePos)
end)
1 Like

In your “reload” function within the local script, you fire the “RemoteShoot” RemoteEvent:

local function reload()
	RemoteShoot:FireServer()
end

No arguments are provided, this could potentially be the issue.

The reload function isn’t connected to any events currently, so it can’t be the problem

Hmm. Well this is quite the odd thing isn’t it. Could you try printing mousePos at the very start of the .OnServerEvent event?

It starts out normal… Then it starts printing out nil…

1 Like

Could you try adding a print for mousePos on the client now? ( Above the :FireServer method )


I have a nil from the server, but not a nil from the client.

--Updated Code

Tool.Activated:Connect(function()
	local mousePos = mouse.Hit.p
	
	if mousePos == nil then
		print("mousePos == nil") return
	end
	
	print(mousePos, "From Client!")
	RemoteShoot:FireServer(mousePos)
end)
1 Like

Hmm, well if you look at the bottom of your output. You’ll notice it prints “nil from server” three times. That’s not the problem though, the problem is that it should print it in pattern as such:

"nil from client"
"nil from server"
"nil from client"
"nil from server"
"nil from client"
"nil from server"

and so on…

You can see the pattern more clearly above in the output. How first it prints the mousePos from the client, and below it, the server’s.

Try pressing Ctrl+Shift+F to look for any other scripts that may contain the “RemoteShoot” RemoteEvent.

Also, in the server script, you should probably already be checking whether or not the mousePos variable is a Vector3 type as such:

if (typeof(mousePos) ~= "Vector3") then
    return
end
1 Like

Wait a minute…

local function reload()
	RemoteShoot:FireServer()
end

RemoteShoot is supposed to be RemoteReload… Thank you for your help!

1 Like

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