Attempt to perform arithmetic (sub) on Instance and Vector3

Hello,
I’ve encountered this error and has been searching devforum for the answer, yet I can’t seem to find the error. I’m trying to make a throwable knife using raycast. But, I believe it thinks the mouse position is an instance. How it works is…

How it works
  • Input
  • Fires bindable event
  • Receives events then creates ray and fires client
  • Client receives and creates the visual

Here’s the script of the raycast:

game:GetService("ReplicatedStorage"):WaitForChild("ConnectorEvent").Event:Connect(function(player,mousePos)
	local character = player.Character
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {character}
	
	local Handle =  character.Sword:WaitForChild("Handle")
	local startPos = Handle.Position
	local endPos = (mousePos-startPos).Unit*50
	local raycastResult = workspace:Raycast(startPos, endPos,raycastParams)
	local hitPart = raycastResult.Instance

	if raycastResult then
		if hitPart:FindFirstChild("Humanoid") then
			hitPart.Humanoid:TakeDamage(100)
		end
		game:GetService("ReplicatedStorage"):WaitForChild("ProjectileEvent"):FireAllClients(startPos,endPos)
	end
end)

Thank you for reading.

4 Likes

Would you mind sending an screenshot of the full error?

This is the only subtract operation I can find.
What is mousePos set to? Send a screenshot on where it’s set.

Ok sure the full error is:
ServerScriptService.ConnectorScript:10: attempt to perform arithmetic (sub) on Instance and Vector3
And mouse pos is:
mouse.Hit.p

Would it be in error if I fire the remote event with the player variable in it? Because now I’m thinking it could be because it thinks the player is the mousePos.

(that’s the same thing, the stack traces would be in a screenshot, BUT NOT NEEDED ANYMORE, because i can confirm what’s going on)

Well, it can’t be. The error is saying that you’re trying to do a subtract operation using an Instance and a Vector3. (While you can do Vector3 - Vector3, you can’t subtract from an instance)

An instance is anything on the explorer, so, the workspace, a part, a service, like ReplicatedStorage, etc.

Also Mouse.Hit.Position is better than Mouse.Hit.p (.p is deprecated)

1 Like

Oh.
Yeah uh, this is a BindableEvent. It’s not server - sided, or client - sided, arguments you pass, are just arguments you pass, it’s kind of just a normal event. You’re supposed to fire it with the player argument if you’re listening to a player argument. That’s the issue, a player is an instance.

1 Like