TestService: Exception thrown in your LengthChanged event handler: ProjectileHandler:1146: invalid argument #2 (Vector3 expected, got nil)

I have this random error whenever I use my shotgun tool, im unsure of the error but it seems to happen at this line?

Cast.UserData.BounceData.CurrentAnchorPoint = (TravelCFrame * Cast.UserData.BounceData.AnchorPoint).p

EDIT: I will experiment by using numbers instead of the assigned values to see if it works:

The error says the second argument in whatever function you’re calling is nil instead of a Vector3 object. Make sure the second argument is actually a Vector3 object (print it or something, there’s probably a reason it is nil).

This seems to be from some sort of module, so unless you give us more, we can’t really help.

2 Likes

I don’t know if this will help, but here is the full function.

local function OnRayUpdated(Cast, LastSegmentOrigin, SegmentOrigin, SegmentDirection, Length, SegmentVelocity, CosmeticBulletObject)
	Cast.UserData.LastSegmentOrigin = LastSegmentOrigin
	Cast.UserData.SegmentOrigin = SegmentOrigin
	Cast.UserData.SegmentDirection = SegmentDirection
	Cast.UserData.SegmentVelocity = SegmentVelocity
	local Tick = os.clock() - Cast.UserData.SpinData.InitalTick
	if Cast.UserData.UpdateData.UpdateRayInExtra then		
		if Cast.UserData.UpdateData.UpdateRayInExtra then
			local RayUpdater = Cast.UserData.UpdateData.ExtraRayUpdater
			if not RayUpdaters[RayUpdater] then
				if RayUpdateFolder:FindFirstChild(RayUpdater) then
					RayUpdaters[RayUpdater]	= require(RayUpdateFolder[RayUpdater])
				end
			end
			if RayUpdaters[RayUpdater] then
				RayUpdaters[RayUpdater](Cast, Cast.StateInfo.Delta)
			end
		end
	end
	if not Cast.UserData.CastBehavior.Hitscan and Cast.UserData.HomeData.Homing then
		local CurrentPosition = Cast:GetPosition()
		local CurrentVelocity = Cast:GetVelocity()
		if Cast.UserData.HomeData.LockOnOnHovering then
			if Cast.UserData.HomeData.LockedEntity then
				local TargetHumanoid = Cast.UserData.HomeData.LockedEntity:FindFirstChildOfClass("Humanoid")
				if TargetHumanoid and TargetHumanoid.Health > 0 then
					local TargetTorso = Cast.UserData.HomeData.LockedEntity:FindFirstChild("HumanoidRootPart") or Cast.UserData.HomeData.LockedEntity:FindFirstChild("Torso") or Cast.UserData.HomeData.LockedEntity:FindFirstChild("UpperTorso")
					local DesiredVector = (TargetTorso.Position - CurrentPosition).Unit
					local CurrentVector = CurrentVelocity.Unit
					local AngularDifference = math.acos(DesiredVector:Dot(CurrentVector))
					if AngularDifference > 0 then
						local OrthoVector = CurrentVector:Cross(DesiredVector).Unit
						local AngularCorrection = math.min(AngularDifference, Cast.StateInfo.Delta * Cast.UserData.HomeData.TurnRatePerSecond)
						Cast:SetVelocity(CFrame.fromAxisAngle(OrthoVector, AngularCorrection):vectorToWorldSpace(CurrentVelocity))
					end
				end
			end
		else
			local TargetEntity, TargetHumanoid, TargetTorso = FindNearestEntity(Cast, CurrentPosition)
			if TargetEntity and TargetHumanoid and TargetTorso and TargetHumanoid.Health > 0 then
				local DesiredVector = (TargetTorso.Position - CurrentPosition).Unit
				local CurrentVector = CurrentVelocity.Unit
				local AngularDifference = math.acos(DesiredVector:Dot(CurrentVector))
				if AngularDifference > 0 then
					local OrthoVector = CurrentVector:Cross(DesiredVector).Unit
					local AngularCorrection = math.min(AngularDifference, Cast.StateInfo.Delta * Cast.UserData.HomeData.TurnRatePerSecond)
					Cast:SetVelocity(CFrame.fromAxisAngle(OrthoVector, AngularCorrection):vectorToWorldSpace(CurrentVelocity))
				end
			end
		end
	end
	local TravelCFrame
	if Cast.UserData.SpinData.CanSpinPart then
		if not Cast.UserData.CastBehavior.Hitscan then
			local Position = (SegmentOrigin + Cast.UserData.SpinData.ProjectileOffset)
			if Cast.UserData.BounceData.SuperRicochet then
				TravelCFrame = CFrame.new(Position, Position + SegmentVelocity) * Math.FromAxisAngle(Tick * Cast.UserData.SpinData.InitalAngularVelocity) * Cast.UserData.SpinData.InitalRotation
			else
				if Cast.UserData.BounceData.CurrentBounces > 0 then
					TravelCFrame = CFrame.new(Position, Position + SegmentVelocity) * Math.FromAxisAngle(Tick * Cast.UserData.SpinData.InitalAngularVelocity) * Cast.UserData.SpinData.InitalRotation
				else
					TravelCFrame = CFrame.new(SegmentOrigin, SegmentOrigin + SegmentVelocity) * CFrame.Angles(math.rad(-360 * ((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinX - math.floor((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinX))), math.rad(-360 * ((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinY - math.floor((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinY))), math.rad(-360 * ((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinZ - math.floor((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinZ))))
				end
			end
		else
			TravelCFrame = CFrame.new(SegmentOrigin, SegmentOrigin + SegmentVelocity) * CFrame.Angles(math.rad(-360 * ((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinX - math.floor((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinX))), math.rad(-360 * ((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinY - math.floor((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinY))), math.rad(-360 * ((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinZ - math.floor((os.clock() - Cast.UserData.ShootId / 4) * Cast.UserData.SpinData.SpinZ))))
		end
	else
		TravelCFrame = CFrame.new(SegmentOrigin, SegmentOrigin + SegmentVelocity)
	end
	Cast.RayInfo.CurrentCFrame = TravelCFrame
	Cast.UserData.BounceData.CurrentAnchorPoint = (TravelCFrame * Cast.UserData.BounceData.AnchorPoint).p
	Cast.UserData.BounceData.BounceDeltaTime += Cast.StateInfo.Delta
	if Cast.UserData.BounceData.BounceDeltaTime > Cast.UserData.BounceData.BounceDelay then
		Cast.UserData.BounceData.LastBouncedObject = nil
	end
	if Cast.UserData.BulletParticleData then
		UpdateParticle(Cast, SegmentOrigin, Cast.UserData.LastPosition, SegmentOrigin)
	end
	if Cast.UserData.LaserData.LaserTrailEnabled then
		if Cast.StateInfo.Delta > 0 then
			local Width = Cast.UserData.LaserData.LaserTrailWidth
			local Height = Cast.UserData.LaserData.LaserTrailHeight
			local TrailSegment = Miscs[Cast.UserData.LaserData.LaserTrailShape.."Segment"]:Clone()
			if Cast.UserData.LaserData.RandomizeLaserColorIn ~= "None" then
				if Cast.UserData.LaserData.RandomizeLaserColorIn == "Whole" then
					TrailSegment.Color = Cast.UserData.LaserData.RandomLaserColor
				elseif Cast.UserData.LaserData.RandomizeLaserColorIn == "Segment" then
					TrailSegment.Color = Color3.new(math.random(), math.random(), math.random())
				end
			else
				TrailSegment.Color = Cast.UserData.LaserData.LaserTrailColor
			end
			TrailSegment.Material = Cast.UserData.LaserData.LaserTrailMaterial
			TrailSegment.Reflectance = Cast.UserData.LaserData.LaserTrailReflectance
			TrailSegment.Transparency = Cast.UserData.LaserData.LaserTrailTransparency
			TrailSegment.Size = Cast.UserData.LaserData.LaserTrailShape == "Cone" and Vector3.new(Width, (SegmentOrigin - LastSegmentOrigin).Magnitude, Height) or Vector3.new((SegmentOrigin - LastSegmentOrigin).Magnitude, Height, Width)
			TrailSegment.CFrame = CFrame.new((LastSegmentOrigin + SegmentOrigin) * 0.5, SegmentOrigin) * (Cast.UserData.LaserData.LaserTrailShape == "Cone" and CFrame.Angles(math.pi / 2, 0, 0) or CFrame.Angles(0, math.pi / 2, 0))
			TrailSegment.Parent = Camera
			table.insert(Cast.UserData.LaserData.LaserTrailContainer, TrailSegment)
			if Cast.UserData.LaserData.UpdateLaserTrail then
				Cast.UserData.LaserData.UpdateLaserTrail:Fire(Cast.UserData.LaserData.LaserTrailId, Cast.UserData.LaserData.LaserTrailContainer)
			end
			Thread:Delay(Cast.UserData.LaserData.LaserTrailVisibleTime, function()
				if Cast.UserData.LaserData.LaserTrailFadeTime > 0 then
					local DesiredSize = TrailSegment.Size * (Cast.UserData.LaserData.ScaleLaserTrail and Vector3.new(1, Cast.UserData.LaserData.LaserTrailScaleMultiplier, Cast.UserData.LaserData.LaserTrailScaleMultiplier) or Vector3.new(1, 1, 1))
					local Tween = TweenService:Create(TrailSegment, TweenInfo.new(Cast.UserData.LaserData.LaserTrailFadeTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Transparency = 1, Size = DesiredSize})
					Tween:Play()
					Tween.Completed:Wait()
					if TrailSegment then
						local Index = table.find(Cast.UserData.LaserData.LaserTrailContainer, TrailSegment)
						if Index then
							table.remove(Cast.UserData.LaserData.LaserTrailContainer, Index)
							if Cast.UserData.LaserData.UpdateLaserTrail then
								Cast.UserData.LaserData.UpdateLaserTrail:Fire(Cast.UserData.LaserData.LaserTrailId, Cast.UserData.LaserData.LaserTrailContainer)
							end
						end
						TrailSegment:Destroy()
					end
				else
					if TrailSegment then
						local Index = table.find(Cast.UserData.LaserData.LaserTrailContainer, TrailSegment)
						if Index then
							table.remove(Cast.UserData.LaserData.LaserTrailContainer, Index)
							if Cast.UserData.LaserData.UpdateLaserTrail then
								Cast.UserData.LaserData.UpdateLaserTrail:Fire(Cast.UserData.LaserData.LaserTrailId, Cast.UserData.LaserData.LaserTrailContainer)
							end
						end
						TrailSegment:Destroy()
					end
				end
			end)			
		end
	end
	if Cast.UserData.LightningData.LightningBoltEnabled then
		if Cast.StateInfo.Delta > 0 then
			local Wideness = Cast.UserData.LightningData.BoltWideness
			local Width = Cast.UserData.LightningData.BoltWidth
			local Height = Cast.UserData.LightningData.BoltHeight
			for _, v in ipairs(Cast.UserData.LightningData.BoltCFrameTable) do
				local Cache
				if Cast.UserData.LightningData.BoltShape == "Block" then
					Cache = BlockSegCache
				elseif Cast.UserData.LightningData.BoltShape == "Cylinder" then
					Cache = CylinderSegCache
				elseif Cast.UserData.LightningData.BoltShape == "Cone" then
					Cache = ConeSegCache
				end
				if Cache then
					local Start = (CFrame.new(SegmentOrigin, SegmentOrigin + SegmentDirection) * v).p
					local End = (CFrame.new(LastSegmentOrigin, LastSegmentOrigin + SegmentDirection) * v).p
					local Distance = (End - Start).Magnitude
					local Pos = Start
					for i = 0, Distance, 10 do
						local FakeDistance = CFrame.new(Start, End) * CFrame.new(0, 0, -i - 10) * CFrame.new(-2 + (math.random() * Wideness), -2 + (math.random() * Wideness), -2 + (math.random() * Wideness))
						local BoltSegment = Cache:GetPart()
						if Cast.UserData.LightningData.RandomizeBoltColorIn ~= "None" then
							if Cast.UserData.LightningData.RandomizeBoltColorIn == "Whole" then
								BoltSegment.Color = Cast.UserData.LightningData.RandomBoltColor
							elseif Cast.UserData.LightningData.RandomizeBoltColorIn == "Segment" then
								BoltSegment.Color = Color3.new(math.random(), math.random(), math.random())
							end
						else
							BoltSegment.Color = Cast.UserData.LightningData.BoltColor
						end
						BoltSegment.Material = Cast.UserData.LightningData.BoltMaterial
						BoltSegment.Reflectance = Cast.UserData.LightningData.BoltReflectance
						BoltSegment.Transparency = Cast.UserData.LightningData.BoltTransparency						
						if i + 10 > Distance then
							BoltSegment.CFrame = CFrame.new(Pos, End) * CFrame.new(0, 0, -(Pos - End).Magnitude / 2) * (Cast.UserData.LightningData.BoltShape == "Cone" and CFrame.Angles(math.pi / 2, 0, 0) or CFrame.Angles(0, math.pi / 2, 0))
						else
							BoltSegment.CFrame = CFrame.new(Pos, FakeDistance.p) * CFrame.new(0, 0, -(Pos - FakeDistance.p).Magnitude / 2) * (Cast.UserData.LightningData.BoltShape == "Cone" and CFrame.Angles(math.pi / 2, 0, 0) or CFrame.Angles(0, math.pi / 2, 0))
						end
						if i + 10 > Distance then
							BoltSegment.Size = Cast.UserData.LightningData.BoltShape == "Cone" and Vector3.new(Width, (Pos - End).Magnitude, Height) or Vector3.new((Pos - End).Magnitude, Height, Width)
						else
							BoltSegment.Size = Cast.UserData.LightningData.BoltShape == "Cone" and Vector3.new(Width, (Pos - FakeDistance.p).Magnitude, Height) or Vector3.new((Pos - FakeDistance.p).Magnitude, Height, Width)
						end
						Thread:Delay(Cast.UserData.LightningData.BoltVisibleTime, function()
							if Cast.UserData.LightningData.BoltFadeTime > 0 then
								local DesiredSize = BoltSegment.Size * (Cast.UserData.LightningData.ScaleBolt and Vector3.new(1, Cast.UserData.LightningData.BoltScaleMultiplier, Cast.UserData.LightningData.BoltScaleMultiplier) or Vector3.new(1, 1, 1))
								local Tween = TweenService:Create(BoltSegment, TweenInfo.new(Cast.UserData.LightningData.BoltFadeTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Transparency = 1, Size = DesiredSize})
								Tween:Play()
								Tween.Completed:Wait()
								if BoltSegment ~= nil then
									Cache:ReturnPart(BoltSegment)
								end
							else
								if BoltSegment ~= nil then
									Cache:ReturnPart(BoltSegment)
								end								
							end
						end)
						Pos = FakeDistance.p
					end					
				end
			end
		end
	end
	if not Cast.UserData.Replicate and Cast.UserData.ClientModule.WhizSoundEnabled then
		if not Cast.UserData.Whizzed then	
			local Mag = (Camera.CFrame.p - SegmentOrigin).Magnitude --(Camera.CFrame.p - CosmeticBulletObject.Position).Magnitude		
			if Mag < Cast.UserData.ClientModule.WhizDistance then
				local WhizSound = Instance.new("Sound")
				WhizSound.SoundId = "rbxassetid://"..Cast.UserData.ClientModule.WhizSoundIDs[math.random(1, #Cast.UserData.ClientModule.WhizSoundIDs)]
				WhizSound.Volume = Cast.UserData.ClientModule.WhizSoundVolume
				WhizSound.PlaybackSpeed = Random.new():NextNumber(Cast.UserData.ClientModule.WhizSoundPitchMin, Cast.UserData.ClientModule.WhizSoundPitchMax)
				WhizSound.Name = "WhizSound"
				WhizSound.Parent = SoundService				
				WhizSound:Play()
				Debris:AddItem(WhizSound, WhizSound.TimeLength / WhizSound.PlaybackSpeed)
				Cast.UserData.Whizzed = true
			end
		end
	end
	Cast.UserData.LastPosition = SegmentOrigin
	if CosmeticBulletObject == nil then
		return
	end
	CosmeticBulletObject.CFrame = TravelCFrame
end

I’m mostly unsure why it causes this error in the first place.

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