--function totalMass(model)
--	local total = 0
--	for i, v in pairs (model:GetDescendants()) do
--		if v:IsA("BasePart") or v:IsA("UnionOperation") or v:IsA("MeshPart") then
--			total = total + v:GetMass()
--		end
--	end
--	return total
--end


local function rando1to1() --because we dont want 0
	return math.random(-1, 1)
end

function module.createBullet(car, gun, player, customize, hitPos)
	math.randomseed(tick() + time())
	--speed defaults to 200 studs per sec
	local maxDist = 10000
	local speed = customize.Speed or 50 --how fast the bullet goes (maxDist studs per speed seconds)
	local dist = (hitPos - gun.firePart.Position).Magnitude
	local tPerc = math.clamp(dist/maxDist, 0, 1)
	
	if not hitPos and gun.Name == "LaserGun" then
		--print("X")
		hitPos = gun.firePart.Position + (gun.AIMING.CFrame.LookVector) * maxDist
	elseif not hitPos and gun.Name == "Minigun" then
		local temp = tPerc * 50 --this is so messed up, but I guess works
		hitPos = gun.firePart.Position + ((gun.AIMING.CFrame.LookVector)*maxDist) + Vector3.new(rando1to1() * temp, rando1to1() * temp, rando1to1() * temp)
	elseif dist and dist > maxDist then
		--print("1")
		local tDir = (hitPos - gun.firePart.Position).Unit * maxDist
		hitPos = gun.firePart.Position + tDir
	end
	
	local bullet = Instance.new("Part")
	if gun.Name == "LaserGun" then
		bullet.Shape = Enum.PartType.Cylinder
	end
	bullet.Material = Enum.Material.Neon
	bullet.Size = customize.Size
	bullet.Color = customize.ColorTransition[1]
	bullet.Transparency = customize.Transparency
	bullet.CanCollide = false
	if gun.Name == "LaserGun" then
		bullet.CFrame = CFrame.new(gun.firePart.Position, hitPos) * CFrame.Angles(0, math.rad(90), 0)
	else
		bullet.CFrame = CFrame.new(gun.firePart.Position, hitPos)
	end
	bullet.Parent = car
	
	local existTime = math.clamp(tPerc*speed, 0, speed) --normalize speed, no matter where you aim
	local tween = ts:Create(bullet, TweenInfo.new(existTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Position = hitPos})
	ds:AddItem(bullet, existTime)
	tween:Play()
	transitionColor(bullet, existTime, customize.ColorTransition[2])
end

return module
