Attempt to perform arithmetic (mul) on Vector3 and nil when raycasting

Sorry for my code being messy, still trying to figure this stuff out.
Anyways, the problem is that for some reason when I make the ray, the direction is just nil. I’ve tried using CFrame and .unit, but nothing seems to work. Plus, this doesn’t happen all the time, it happens randomly.

function ToolClass:new_raycast(player, OrginPos, Direction)
	print(Direction)
	local rayCast = Ray.new(OrginPos, -Direction*self.range, self.params)
	print(rayCast)
	--local Result = workspace:Raycast(OrginPos, Direction*self.range, self.params)
	local Result, pos = workspace:FindPartOnRay(rayCast, player.Character, false, true)
	
	local part = Instance.new("Part", workspace)
	part.Anchored = true
	part.CanCollide = false
	part.Position = pos
	
	if Result then
		
		--local target = Result.Instance

		if Result.Parent:FindFirstChild("Humanoid") then
			local realDamage = self:get_damage()
			
			onHit:FireServer(player, Result, realDamage)
			print(Result)
		elseif Result.Parent:IsA("Accessory") then
			table.insert(self.filterTable, Result)
			ToolClass:new_raycast(player, OrginPos, Direction)
		elseif Result.CanCollide == false then
			table.insert(self.filterTable, Result)
			ToolClass:new_raycast(player, OrginPos, Direction)
		end
	end
end

--// when attacking \\--

function ToolClass:attack(player, hitPos, OrginPos)
	
	table.insert(self.filterTable, player.Character)
	self.params.FilterDescendantsInstances = self.filterTable
	self.FilterType = Enum.RaycastFilterType.Exclude
	
	print(hitPos)
	
	local OrginPos = OrginPos--player.Character.HumanoidRootPart.Position
	local targetPos = Vector3.new(hitPos.X, OrginPos.Y, hitPos.Z)
	local Direction = (OrginPos - targetPos).Unit
	--local Direction = CFrame.new(OrginPos, targetPos)
	print(Direction)
	self:new_raycast(player, OrginPos, Direction)
	
	
	
	self.usage -= 1
	self.canActivate = false
	print(self.usage)
end


--------------------------------------------------------------------------------------------------------------------------------

--// on_activation \\--

function ToolClass:on_activation(player, hitPos, OrginPos)
	if self.canActivate then --// if you're able to activate the tool \\--
		if self.usage > 0 or self.usage <= self.infUse_Number then --// and it's above 0 and below the infinate use number //--
			self.filterTable = {}
			--rayCast_Event:FireServer(player, hitPos)
			self:attack(player, hitPos, OrginPos) --// then attack \\--
			
			if not self:check_usability() then --// check if the tool still has any usage left \\--
				
				--// if so start the cooldown  and print a slogan after the cooldown is done \\--
				self:cool_down()
				print("lets rock")
				
			else --// else return that the tool has no more usage so that the invetory can reset it \\--
				return true
			end
			
		end
	end
	
	return false --// return false if everything worked \\--
end

What I think is happening, since the unit of a vector3 with only nil (as well as 0) is nan.
image
The OriginPos must be equal to targetPos since that is the only way that it will produce a 0 vector without setting off some sort of error earlier.

Also why are you setting OriginPos = OriginPos?

Ok, i think I fixed the problem. The issue was that I was using ToolClass instead of self. Thanks.

1 Like

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