Really weird bug starts to fix itself randomly

  1. What do you want to achieve?
    I wanted to create a bullet tracer system. It would work by creating a beam and moving the origin attachment to the end attachment with a tween.

  2. What is the issue?
    When I shoot the gun, the origin attachment suddenly goes into the abyss??? I looked into the code of my tracer event, but it doesn’t seem to be the issue.

local enabled												= true
local beamLifetime											= .1
local beamDeleteTime										= .25
local info													= TweenInfo.new(beamLifetime, Enum.EasingStyle.Linear, Enum.EasingDirection.In)

local function unpackNewClone(color: Color3)
	local newTracer = BaseTracer:Clone()
	
	local source, point = newTracer:WaitForChild("origin"), newTracer:WaitForChild("end")
	local beam = newTracer:WaitForChild("Beam")
	
	source.Parent = tracerPart
	point.Parent = tracerPart
	beam.Parent = tracerPart
	
	newTracer:Destroy()
	
	return source, point, beam
end

BulletEvent.OnClientEvent:Connect(function(origin: Vector3, point: Vector3, color: Color3)
	if not enabled then
		return
	end
	
	tracerPart.Position = origin
	local source, dest, beam = unpackNewClone(color)
	
	source.WorldCFrame = CFrame.new(origin, point)
	dest.WorldPosition = point
	
	local currrentTween = TweenService:Create(source, info, {WorldPosition = point})
	task.wait()
	currrentTween:Play()
	
	Debris:AddItem(source, beamDeleteTime)
	Debris:AddItem(dest, beamDeleteTime)
	Debris:AddItem(beam, beamDeleteTime)
end)

But then, if you wait a few minutes it suddenly starts to fix itself??? I think the problem is with the viewmodel’s bopping. I tested standing still and the position is changing, so I think that’s the problem. The problem is that, I don’t understand any of the math with the viewmodel bopping.

--Edited script of the viewmodel handler
local SpringModule				= require(ClientModules:WaitForChild("SpringModule"))

local function Bob(addition)
	return math.sin(tick() * addition * 1.3) * 0.5
end

local function update()
	LookUpDown:FireServer(mouse.Hit.Position, Camera.CFrame.Position)
end

local function setArmsToCam(dt: number)
	if not primaryPart then
		return
	end
	
	local Delta = UserInputService:GetMouseDelta()
	
	swaySpring:shove(Vector3.new(-Delta.X/500, Delta.Y/500, 0))
	bobSpring:shove(Vector3.new(Bob(5), Bob(10), Bob(5)) / 10 * (primaryPart.Velocity.Magnitude) / 10)
	
	local UpdatedSway = swaySpring:update(dt)
	local UpdatedBob = bobSpring:update(dt)
	
	local cameraCframe = (Camera.CFrame*cameraOffset)
	
	Arms:SetPrimaryPartCFrame(cameraCframe*
		CFrame.new(UpdatedSway.X, UpdatedSway.Y, 0)*
		CFrame.new(UpdatedBob.X, UpdatedBob.Y, 0)
	)
end

--Current spring module I'm using:
-- Constants

local ITERATIONS	= 8

-- Module

local SPRING	= {}

-- Functions 

function SPRING.new(self, mass, force, damping, speed)

	local spring	= {
		Target		= Vector3.new();
		Position	= Vector3.new();
		Velocity	= Vector3.new();

		Mass		= mass or 5;
		Force		= force or 50;
		Damping		= damping or 4;
		Speed		= speed  or 4;
	}

	function spring.getstats(self)
		return self.Mass, self.Force, self.Damping, self.Speed
	end

	function spring.changestats(self, mass, force, damping, speed)
		self.Mass = mass or self.Mass
		self.Force = force or self.Force
		self.Damping = damping or self.Damping
		self.Speed = speed or self.Speed
	end

	function spring.shove(self, force)
		local x, y, z	= force.X, force.Y, force.Z
		if x ~= x or x == math.huge or x == -math.huge then
			x	= 0
		end
		if y ~= y or y == math.huge or y == -math.huge then
			y	= 0
		end
		if z ~= z or z == math.huge or z == -math.huge then
			z	= 0
		end
		self.Velocity	= self.Velocity + Vector3.new(x, y, z)
	end

	function spring.update(self, dt)
		local scaledDeltaTime = dt * self.Speed / ITERATIONS
		for i = 1, ITERATIONS do
			local iterationForce= self.Target - self.Position
			local acceleration	= (iterationForce * self.Force) / self.Mass

			acceleration		= acceleration - self.Velocity * self.Damping

			self.Velocity	= self.Velocity + acceleration * scaledDeltaTime
			self.Position	= self.Position + self.Velocity * scaledDeltaTime
		end

		return self.Position
	end

	return spring
end
-- Return

return SPRING

Here is the viewmodel that im using:
fpa.obj (16.4 KB)

  1. What solutions have you tried so far?
    I have no idea how to solve this. I want to keep the viewmodel bopping into my game tho.
1 Like